使用说明
简单MCP
一个用于创建的简单TypeScript库 主控程序 (模型上下文协议)服务器。
特性
- 简单API:使用最少的代码创建MCP服务器
- 类型安全:完全集成TypeScript
- 参数验证:使用Zod进行内置验证
- MCP兼容:完全实现模型上下文协议
安装
npm install simple-mcp
快速启动
import { McpServer } from 'simple-mcp';
import { z } from 'zod';
// Create a server instance
const server = new McpServer({ name: 'my-server' });
// Register the tool with the server
server.tool({
name: 'greet',
parameters: {
name: z.string().describe('Person\'s name')
},
execute: async ({ name }) => {
return {
content: [
{
type: 'text',
text: `Hello, ${name}! Nice to meet you.`
}
]
};
}
});
// Start the server
server.start({ transportType: 'stdio' });
基于类的实现
您还可以使用类实现MCP工具:
import { McpServer, type McpTool } from 'simple-mcp';
import { z, ZodObject } from 'zod';
const parameters = {
name: z.string().describe('The name is required'),
};
class GreetTool implements McpTool<typeof parameters> {
public readonly name = 'greet';
public readonly parameters = parameters;
public async execute({ name }: z.infer<ZodObject<typeof this.parameters>>) {
return {
content: [
{
type: 'text',
text: `Hello, ${name}! Nice to meet you.`,
},
],
};
}
}
// Initialize a new MCP server with the name 'greet-server'
const server = new McpServer({ name: 'greet-server' });
// Create an instance of the GreetTool class
const greetTool = new GreetTool();
// Register the tool with the server
server.tool(greetTool);
// Start the server using stdio as the transport method
server.start({ transportType: 'stdio' });
例子
看看 示例目录 有关更完整的示例:
贡献
欢迎投稿!请随时打开问题或提交pull请求。