@easbot/mcp
v0.3.23
Published
MCP (Model Context Protocol) integration library for EASBOT ecosystem
Maintainers
Readme
English | 中文
@easbot/mcp
MCP (Model Context Protocol) 集成库,为 EASBot 生态系统提供完整的 MCP 服务器和客户端实现。
基于 @modelcontextprotocol/sdk 构建,提供 stdio 和 HTTP 两种传输方式,支持工具注册和动态管理。
特性
- 双传输支持: 支持 stdio(本地进程)和 HTTP(远程服务)两种传输方式
- 服务器端实现: 创建和管理 MCP 服务器,向客户端提供工具和资源
- 客户端实现: 连接到 MCP 服务器,调用远程工具和读取资源
- 工具注册: 全局工具注册表,支持动态注册/注销工具
- OAuth 认证: 支持 OAuth 2.0 认证,确保安全连接
- 配置驱动: 支持从配置文件批量创建服务器实例
- 完整的类型定义: 提供完整的 TypeScript 类型定义
快速开始
创建 MCP 服务器
import { HttpServerAdapter, ToolRegistry } from '@easbot/mcp';
import { z } from 'zod';
// 调用方自己 new ToolRegistry 并注册工具(构造注入模式)
const tools = new ToolRegistry();
tools.register({
name: 'my-tool',
description: '一个示例工具',
inputSchema: z.object({ name: z.string() }),
handler: async (args) => ({ content: [{ type: 'text', text: `Hello, ${args.name}!` }] })
});
// 创建并启动服务器(注入 tools)
const server = new HttpServerAdapter({
id: 'my-server',
name: 'My MCP Server',
version: '1.0.0',
transportType: 'http',
url: 'http://localhost:3000'
}, tools);
await server.start();连接到 MCP 服务器
import { MCPClient } from '@easbot/mcp';
const client = new MCPClient();
// 连接本地服务器
await client.connect('filesystem', {
type: 'local',
command: ['npx', '-y', '@modelcontextprotocol/server-filesystem', '/tmp']
});
// 查看客户端信息
console.log(client.info);
// 调用工具
const result = await client.callTool('read_file', { path: '/tmp/test.txt' });
// 断开连接
await client.disconnect();从配置创建服务器
import { createServerFromConfig, createServersFromConfigMap, ToolRegistry } from '@easbot/mcp';
// 调用方自己 new ToolRegistry
const tools = new ToolRegistry();
tools.register({ name: 'shared-tool', handler: ... });
// 创建单个服务器
const server = await createServerFromConfig('my-server', {
type: 'local',
command: ['node', 'server.js']
}, tools);
// 从配置映射创建多个服务器(共享同一 ToolRegistry)
const servers = await createServersFromConfigMap({
'server-1': { type: 'local', command: ['node', 'server1.js'] },
'server-2': { type: 'remote', url: 'https://mcp.example.com' }
}, tools);开发
# 安装依赖
pnpm install
# 构建
pnpm build
# 测试
pnpm test
# 类型检查
pnpm type-check
# 代码检查
pnpm lint
# 代码格式化
pnpm format许可证
MIT
