Skip to content

MCP Plugin

集成 Model Context Protocol (MCP) 工具,将 MCP Tools 映射为 A2A Skills。

使用

typescript
import { createAgentServer, createMCPPlugin } from '@multi-agent/a2a'

const server = createAgentServer(config)
  .use(createMCPPlugin({
    servers: [
      {
        name: 'filesystem',
        transport: 'stdio',
        command: 'npx',
        args: ['-y', '@modelcontextprotocol/server-filesystem', '/workspace']
      }
    ]
  }))

await server.start()

配置多个 Server

typescript
createMCPPlugin({
  skillPrefix: 'tool',
  servers: [
    // 文件系统
    {
      name: 'fs',
      transport: 'stdio',
      command: 'npx',
      args: ['-y', '@modelcontextprotocol/server-filesystem', '/workspace']
    },
    // GitHub
    {
      name: 'github',
      transport: 'stdio',
      command: 'npx',
      args: ['-y', '@modelcontextprotocol/server-github'],
      env: { GITHUB_TOKEN: process.env.GITHUB_TOKEN }
    },
    // 数据库
    {
      name: 'db',
      transport: 'stdio',
      command: 'npx',
      args: ['-y', '@modelcontextprotocol/server-postgres'],
      env: { DATABASE_URL: process.env.DATABASE_URL }
    }
  ],
  connectTimeout: 30000,
  callTimeout: 60000,
  autoReconnect: true,
})

调用 MCP Tools

typescript
const stream = await client.call('mcp_fs_read_file', {
  path: '/workspace/README.md'
})

for await (const msg of stream) {
  if (msg.type === 'done') {
    console.log('内容:', msg.data.content)
  }
}

HTTP 传输

连接远程 MCP Server:

typescript
createMCPPlugin({
  servers: [
    {
      name: 'remote',
      transport: 'http',
      url: 'https://mcp.example.com/server'
    }
  ]
})

Plugin API

typescript
// 获取连接状态
mcpPlugin.getConnectedServers()
// [{ name: 'fs', connected: true }]

// 获取所有 MCP Skills
mcpPlugin.getMCPSkills()

// 获取指定 Server 的工具
mcpPlugin.getServerTools('fs')

// 手动连接/断开
await mcpPlugin.connectServer('github')
await mcpPlugin.disconnectServer('github')
await mcpPlugin.disconnectAll()

// 检查是否为 MCP Skill
mcpPlugin.isMCPSkill('mcp_fs_read_file')  // true

动态 AgentCard

MCP Plugin 自动更新 AgentCard:

typescript
const card = await client.getAgentCard()

console.log(card.skills)
// [
//   { name: 'custom', ... },
//   { name: 'mcp_fs_read_file', description: 'Read file' },
//   { name: 'mcp_fs_write_file', description: 'Write file' },
//   ...
// ]

监控连接

typescript
setInterval(() => {
  const servers = mcpPlugin.getConnectedServers()
  for (const s of servers) {
    if (!s.connected) {
      console.warn(`${s.name} disconnected`)
    }
  }
}, 10000)

MIT Licensed