跳转到主要内容
AG-Kit 为模型上下文协议 (MCP) 提供全面支持,既能无缝集成外部工具与服务,又能将 AG-Kit 工具暴露给其他兼容 MCP 的应用程序。

AG-Kit MCP 支持

客户端实现

连接现有 MCP 服务器并使用其工具:
import { createMCPToolkit } from '@ag-kit/tools';
import { Agent } from '@ag-kit/agents';

const toolkit = await createMCPToolkit({
  filesystem: {
    command: 'npx',
    args: ['@modelcontextprotocol/server-filesystem', './workspace']
  }
});

const tools = toolkit.getTools();
const agent = new Agent({
  name: 'mcp-agent',
  model: provider,
  tools,
  instructions: 'You can access filesystem tools via MCP.'
});

服务端实现

将 AG-Kit 工具暴露为 MCP 服务器:
import { AGKitMCPServer } from '@ag-kit/tools/mcp';
import { tool } from '@ag-kit/tools';
import { z } from 'zod';

const weatherTool = tool(
  async ({ city }) => {
    return { city, temperature: 22, condition: 'sunny' };
  },
  {
    name: 'get_weather',
    description: 'Get weather information',
    schema: z.object({
      city: z.string().describe('City name')
    })
  }
);

const server = new AGKitMCPServer({
  name: 'weather-service',
  version: '1.0.0'
});

server.registerTool(weatherTool);
await server.run({ type: 'stdio' });

连接类型

  • 标准输入输出: 本地命令行工具
  • HTTP: 远程网络服务
  • 内存通信: 测试与进程内通信

核心特性

  • 工具发现: 自动注册与发现工具
  • 类型安全: 强类型化的工具定义
  • 多传输协议: 支持标准输入输出、HTTP 和内存连接
  • 错误处理: 内置错误处理与恢复机制

后续步骤