模型上下文协议服务器框架

MCP Factory是一个可扩展的模型上下文协议(MCP)服务器框架,具有服务插件架构,允许开发人员通过实现插件来添加新的API服务,适用于开发集成多工具服务的MCP客户端。

  • by decagondev
  • Python
  • 本地部署
  • 9 星标
  • 更新于
在 GitHub 查看 decagondev/MCP-Factory

使用说明

MCP工厂

可扩展 模型上下文协议(MCP) 服务器框架 服务插件架构.内置 FastMCP Python 3.13。以美国国家航空航天局APOD(每日天文图片)服务为参考实施的船只。

通过实现插件添加新的API服务——您的工具会自动出现在每个MCP客户端中。分叉仓库,从坚实的solid基础构建自己的生产MCP服务器。

捆绑服务:美国国家航空航天局APOD

附带的APOD服务演示了具有以下功能的插件模式:

  • 获取今天的照片 --获取当前当天的天文图片,包括标题、解释和媒体链接。
  • 按日期获取照片 --通过提供日期从档案中检索任何APOD条目(1995-06-16至今)。
  • 获取随机照片 --从美国国家航空航天局30多年的档案中随机发现一个APOD。
  • 著名太空日期资源 --APOD档案中精心策划的标志性太空探索日期列表。

工具

工具 说明
get_todays_space_photo 返回今天的APOD,包括标题、解释、媒体类型和URL
get_space_photo_by_date 返回特定日期的APOD(YYYY-MM-DD 格式)
get_random_space_photo 从完整存档中返回随机APOD

资源

URI 描述
space://events/famous-dates 著名太空探索日期精选列表

先决条件

安装

git clone <your-repo-url>
cd mcp-factory
uv sync

用法

独立运行服务器

uv run main.py

服务器通过以下方式进行通信 标准 传输,这是MCP客户端集成的标准。

添加到游标IDE

将以下内容添加到光标MCP设置中(.cursor/mcp.json):

{
  "mcpServers": {
    "mcp-factory": {
      "command": "uv",
      "args": [
        "run",
        "--directory",
        "/absolute/path/to/mcp-factory",
        "main.py"
      ]
    }
  }
}

替换 /absolute/path/to/mcp-factory 在您的计算机上显示此项目的实际路径。

添加到Claude桌面

将以下内容添加到您的Claude Desktop配置中(claude_desktop_config.json):

{
  "mcpServers": {
    "mcp-factory": {
      "command": "uv",
      "args": [
        "run",
        "--directory",
        "/absolute/path/to/mcp-factory",
        "main.py"
      ]
    }
  }
}

API密钥

捆绑的APOD服务使用美国国家航空航天局的 DEMO_KEY 默认情况下,其速率限制有限(每小时30个请求,每个IP每天50个请求)。对于更重的使用, 请求一个免费的API密钥 并设置 NASA_API_KEY 环境变量:

export NASA_API_KEY="your-key-here"

项目结构

mcp-factory/
├── main.py                         # Thin entry point — starts the MCP server
├── mcp_factory/                      # Core package
│   ├── __init__.py
│   ├── config.py                   # Global config (server name)
│   ├── server.py                   # Builds FastMCP via ServiceRegistry
│   └── services/                   # Service plugin architecture
│       ├── __init__.py
│       ├── base.py                 # ABCs: BaseAPIClient, BaseFormatter, ServicePlugin
│       ├── registry.py             # ServiceRegistry factory
│       └── apod/                   # APOD service plugin
│           ├── __init__.py         # ApodService (registers tools + resources)
│           ├── config.py           # APOD-specific constants and API key
│           ├── client.py           # ApodClient (extends BaseAPIClient)
│           ├── formatter.py        # ApodFormatter (extends BaseFormatter)
│           └── validation.py       # APOD date validation
├── tests/                          # Unit + E2E tests (pytest)
│   ├── test_base.py                # ABC contract tests
│   ├── test_registry.py            # ServiceRegistry factory tests
│   ├── test_apod_config.py         # APOD config constants
│   ├── test_apod_client.py         # ApodClient with mocked HTTP
│   ├── test_apod_formatter.py      # ApodFormatter output
│   ├── test_apod_validation.py     # Date validation
│   └── test_e2e.py                 # Full server bootstrap + tool execution
├── templates/                      # Copy-paste service boilerplate
│   ├── README.md                   # Template usage instructions
│   └── service_template/           # Complete service plugin skeleton
├── docs/                           # Guides, architecture, and workshop
│   ├── ARCHITECTURE.md             # System design and SOLID mapping
│   ├── ADDING-A-SERVICE.md         # Step-by-step new service guide
│   ├── ADDING-TOOLS.md             # Tool and resource creation guide
│   ├── FORKING-GUIDE.md            # Using this repo as a template
│   ├── DEVELOPMENT-WORKFLOW.md     # Git, testing, and review workflow
│   ├── LETS-BUILD-AN-MCP-SERVER.md # Original step-by-step tutorial
│   └── index.html                  # Slide deck presentation
├── CLAUDE.md                       # AI agent context (Claude Code, etc.)
├── pyproject.toml                  # Project metadata and dependencies
├── uv.lock                         # Locked dependency versions
├── .python-version                 # Python version (3.13)
├── .gitignore                      # Git ignore rules
└── README.md                       # This file

使用新的API服务进行扩展

此服务器使用 服务插件架构.每个API都是一个独立的插件 mcp_factory/services/.要添加新的API:

  1. 创建新目录: mcp_factory/services/your_api/
  2. 实现客户端扩展 BaseAPIClient (处理HTTP)
  3. 实现扩展的格式化程序 BaseFormatter (处理Markdown输出)
  4. 使用创建服务类 register(mcp) 注册工具的方法
  5. 将其添加到注册表中 mcp_factory/server.py:
from mcp_factory.services.your_api import YourApiService

registry.add(YourApiService())

您的新工具会自动显示,无需更改其他文件。

文档

对于开发者

指南 说明
建筑学 系统设计、插件生命周期、数据流、SOLID原则、模块图
添加服务 添加新API服务插件的分步指南
添加工具 如何向现有服务添加工具和资源
叉车指南 将此仓库用作新MCP服务器的模板
开发工作流程 Git工作流程、测试策略、代码审查清单
教程 原始的分步MCP服务器教程

对于AI代理

资源 描述
CLAUDE.md Claude Code和代理框架的根级上下文文件
.cursor/rules/architecture.mdc Cursor AI的始终在线架构上下文
.cursor/rules/extending-services.mdc 扩展服务时触发的分步指令
.cursor/rules/mcp-factory-tools.mdc 捆绑APOD服务的工具路由规则

锅炉板模板

复制粘贴就绪服务框架 templates/service_template/。参见 模板/README.md 供使用。

依赖项

  • mcp[cli] --支持CLI的模型上下文协议SDK
  • httpx -用于外部API调用的异步HTTP客户端

许可证

这个项目是供教育和个人使用的。捆绑的APOD服务使用来自 NASA的APOD API.