MCP服务器模板

为macOS应用和命令行工具提供构建模型控制协议(MCP)服务器的模板库,包含Swift包结构和命令行示例。

  • by adamwulf
  • Swift
  • 本地部署
  • 17 星标
  • 更新于
在 GitHub 查看 adamwulf/mcp-template

使用说明

MCP模板

一个模板存储库,为构建macOS应用程序和命令行工具的模型控制协议(MCP)服务器提供了基础。

概述

MCP模板是希望在项目中实现MCP服务器的开发人员的起点。此模板演示了如何使用 mcp-swift-sdk 以最小的方式,使其更容易理解MCP集成的基础知识。它包括一个用于集成到其他项目的库模板和一个简单的命令行示例来说明基本用法。

目的

此存储库旨在:

  • 显示如何使用的参考实现 mcp-swift-sdk
  • 一个可以分叉或克隆的模板,作为您自己的MCP服务器实现的基础
  • 用最少的代码演示核心MCP概念的基本示例

特性

当前和计划中的功能包括:

  • [x] Swift包的基本结构
  • [x] 命令行“hello world”示例工具
  • [x] 通过命令行stdio直接与MCP交互 run 命令
  • []App Store安全命令行stdio→ 独立Mac应用程序通信
  • []包中的SSE服务器→ 基于SSE的MCP的命令行应用程序示例

安装

Swift包管理器

将以下内容添加到您的 Package.swift 文件:

dependencies: [
    .package(url: "https://github.com/your-username/mcp-template.git", branch: "main"),
]

然后将依赖关系添加到目标:

.target(
    name: "YourTarget",
    dependencies: [
        .product(name: "EasyMCP", package: "mcp-template")
    ]
),

用法

基础示例

import EasyMCP

// Create an instance of EasyMCP
let mcp = EasyMCP()

// Register a tool
try await mcp.register(tool: Tool(
    name: "helloPerson",
    description: "Returns a friendly greeting message",
    inputSchema: [
        "type": "object",
        "properties": [
            "name": [
                "type": "string",
                "description": "Name of the person to say hello to",
            ]
        ],
        "required": ["name"]
    ]
)) { input in
    // It's an async closure, so you can await whatever you need to for long running tasks
    await someOtherAsyncStuffIfYouWant()
    // Return your result and flag if it is/not an error
    return Result(content: [.text(hello(input["name"]?.stringValue ?? "world"))], isError: false)
}

// Start the MCP server for full MCP interaction
try await mcp.start()
try await mcp.waitUntilComplete()

命令行示例

该包包括一个命令行可执行文件,名为 mcpexample 演示基本用法:

# Run the basic hello example
mcpexample hello

# Start the MCP server to handle MCP protocol communications
mcpexample run

项目结构

目标

  1. EasyMCP (图书馆)

    • MCP服务器的最小模板实现
    • 演示与MCP协议的基本集成
    • 展示如何利用官方 mcp-swift-sdk
    • 包括一个简单的工具示例(helloworld)
  2. mcpexample (可执行)

    • 使用EasyMCP库的简单命令行示例
    • 包括hello命令和run命令
    • run命令使用stdio传输启动完整的MCP服务器
    • 用途 ArgumentParser 用于CLI参数处理
  3. EasyMCP测试 (测试目标)

    • EasyMCP库功能的模板测试
    • 包括hello函数的基本测试

依赖项

  • 快速参数分析器 (1.3.0+)-用于CLI参数处理
  • mcp swift sdk (分支:“功能/等待完成”)-MCP实现的自定义分支,增加了等待MCP服务器完成的能力

发展

要将此模板用于您自己的MCP服务器,请执行以下操作:

  1. 克隆或分叉存储库
  2. 构建包以验证一切正常:
    swift build
    
  3. 运行测试:
    swift test
    
  4. 修改EasyMCP实现以添加您的自定义功能
  5. 扩展命令行示例或创建自己的Mac应用程序

使用它也可能有所帮助 MCP检查员 诊断和调试自定义MCP服务器。

$ npx @modelcontextprotocol/inspector

测试MCP命令行可执行文件

要使用MCP检查器测试和调试MCP服务器,请执行以下操作:

  1. 在Xcode中构建命令行可执行文件
  2. 通过转到Xcode找到可执行文件→ 产品→ 在Finder中显示构建文件夹
  3. 从该目录复制可执行文件的绝对路径
  4. 使用 MCP检查员 测试您的服务器
  5. 打开终端并运行:
    npx @modelcontextprotocol/inspector <absolute_path_to_your_executable> run
    
  6. 打开浏览器到输出中显示的端口:
    🔍 MCP Inspector is up and running at http://localhost:5173 🚀
    
  7. 按下MCP检查器界面中的连接按钮
  8. 打开活动监视器并搜索您的可执行文件名
  9. 仅验证检查器和工具的单个实例是否正在运行
  10. 在Xcode中→ 调试→ 附加到进程→ 在顶部找到您的可执行文件名并附加
  11. 在终端中,运行 tail -n 20 -f ~/Library/Logs/Claude/mcp*.log
  12. 现在,您可以通过检查器与服务器交互,同时在Xcode中点击断点!

此设置提供了一个强大的调试环境,您可以在其中:

  • 通过Inspector的UI测试MCP服务器的功能
  • 在代码中设置断点以跟踪执行
  • 在操作过程中检查变量和状态
  • 实时调试资源、提示和工具实现

有关更多调试提示,请访问 MCP调试 在Anthropic的modelcontextprotocol.io网站上。

许可证

麻省理工学院许可。

致谢