MCP服务器模板

一个最小化的Model Context Protocol (MCP)服务器模板,用于快速构建自定义MCP服务器。

  • by marthakelly
  • Python
  • 本地部署
  • 更新于
在 GitHub 查看 marthakelly/mcp-template

使用说明

极简MCP服务器模板

一个最小化的模型上下文协议(MCP)服务器模板。克隆此模板以构建您自己的MCP服务器。

包含内容

  • server.py - 一个带有示例工具的基本MCP服务器
  • requirements.txt - Python 依赖项
  • 这个README文件

快速入门

1. 克隆或分叉此仓库

git clone https://github.com/YOUR-USERNAME/mcp-template.git
cd mcp-template

2. 安装依赖项

pip install -r requirements.txt

3. 测试服务器

python server.py

服务器正在运行并等待来自Claude Desktop的输入。

4. 添加到Claude桌面

编辑您的Claude桌面配置文件:

macOS(苹果电脑操作系统): ~/Library/Application Support/Claude/claude_desktop_config.json Windows: %APPDATA%/Claude/claude_desktop_config.json

添加此配置:

{
  "mcpServers": {
    "simple-server": {
      "command": "python",
      "args": ["/absolute/path/to/mcp-template/server.py"]
    }
  }
}

替换 /absolute/path/to/mcp-template/ 与实际路径一致。

5. 重启Claude桌面版

退出并重新启动 Claude 桌面版。您的 MCP 服务器现在可用。

如何修改

添加您自己的工具

编辑 server.py 并在其中添加新工具 list_tools() 功能:

@server.list_tools()
async def list_tools() -> list[Tool]:
    return [
        Tool(
            name="your_tool_name",
            description="What your tool does",
            inputSchema={
                "type": "object",
                "properties": {
                    "param1": {
                        "type": "string",
                        "description": "Parameter description"
                    }
                },
                "required": ["param1"]
            }
        )
    ]

处理工具调用

在其中添加你的逻辑 call_tool() 功能:

@server.call_tool()
async def call_tool(name: str, arguments: dict) -> list[TextContent]:
    if name == "your_tool_name":
        # Your logic here
        result = do_something(arguments.get("param1"))
        return [TextContent(type="text", text=result)]

连接到API或数据库

导入所需内容,并在您的工具处理器中调用它:

import requests

@server.call_tool()
async def call_tool(name: str, arguments: dict) -> list[TextContent]:
    if name == "fetch_data":
        response = requests.get("https://api.example.com/data")
        return [TextContent(type="text", text=response.text)]

资源

许可证

麻省理工学院(MIT)——随你如何使用。