MCP Server-Client Example

MCP Server-Client Example

这个仓库托管了一个TypeScript演示,展示了MCP Server实现的模型上下文协议(MCP),用于资源分享和检索的基本服务器-客户端通信。

简介

这个项目展示了如何使用 TypeScript SDK 实现 Model Context Protocol (MCP) 进行服务器-客户端通信。

准备工作

  • Node.js (v16+)
  • npm

安装

npm install

快速启动

  1. 启动服务器

    npm run start:server
    
  2. 启动客户端(在新终端中):

    npm run start:client
    

客户端会自动连接到服务器、请求资源列表并读取示例资源。

项目结构

src/
  ├── index.ts    # 服务器实现
  └── client.ts   # 客户端实现

功能概览

  • 资源列表查询
  • 资源内容读取
  • 基本错误处理

添加新资源

编辑 src/index.ts 文件中的资源列表处理器:

server.setRequestHandler(ListResourcesRequestSchema, async () => {
  return {
    resources: [
      { uri: "file:///example.txt", name: "Example Resource" },
      // 在此处添加新资源
    ],
  };
});

开发命令

  • npm run start:server - 启动服务器
  • npm run start:client - 启动客户端

许可证

ISC