MCP服务器模板

这是一个用于创建Model Context Protocol (MCP)服务器的TypeScript启动模板,提供基本的设置和示例工具实现,帮助开发者快速构建自己的MCP服务器。

  • by yhwancha
  • TypeScript
  • 本地部署
  • 更新于
在 GitHub 查看 yhwancha/mcp_server_starter_typescript

使用说明

MCP服务器TypeScript入门

这是一个使用TypeScript创建模型上下文协议(MCP)服务器的入门模板。它提供了一个基本设置和一个示例工具实现,以帮助您开始构建自己的MCP服务器。

特性

  • TypeScript配置
  • 基本MCP服务器设置
  • 示例工具实现
  • 类型安全开发环境

入门指南

按照以下步骤创建自己的MCP服务器:

# Create a new directory for your project
mkdir <project_name>
cd <project_name>

# Initialize a new npm project
npm init -y

# Install dependencies
npm install @modelcontextprotocol/sdk zod
npm install -D @types/node typescript

# Create source directory and main file
mkdir src
touch src/index.ts

项目结构

.
├── src/
│   └── index.ts    # Main server implementation
├── package.json    # Project dependencies and scripts
└── tsconfig.json   # TypeScript configuration

发展

  1. 在中实施您的工具 src/index.ts
  2. 构建项目:
    npm run build
    

添加新工具

要添加新工具,请使用 server.tool() 方法。例子:

server.tool(
  "tool-name",
  "tool-description",
  { 
    // Define your tool's parameters using Zod schema
    param: z.string().describe("parameter description") 
  },
  async ({ param }) => {
    // Implement your tool logic here
    return {
      content: [
        {
          type: "text",
          text: `Tool executed with parameter: ${param}`,
        },
      ],
    };
  },
);

MCP服务器配置

您可以在项目中配置多个MCP服务器。以下是一个配置示例:

{
    "mcpServers": {
        "<server_name>": {
            "command": "<command>",
            "args": [
                "--directory",
                "/ABSOLUTE/PATH/TO/PARENT/FOLDER/<project_name>",
                "run",
                "<script_name>"
            ]
        }
    }
}

此配置指定:

  • <server_name>:MCP服务器的唯一标识符
  • command:运行脚本的命令(例如。, python, node, uv)
  • args:命令行参数数组
    • --directory:设置脚本的工作目录
    • 其他特定于您的命令和脚本需求的参数

您可以通过向中添加更多条目来配置多个服务器 mcpServers 对象。

许可证

国际协调委员会