Skip to main content
Glama

mcpforge

用 5 行 Python 代码发布一个 MCP 服务器。

Python License: MIT Status MCP

Before                              After
──────                              ─────
~200 lines of                       @serve
JSON-RPC stdio                      class MarketTools:
boilerplate, schema                     @tool
generation, error                       def latest_price(self, symbol: str) -> float: ...
handling, lifecycle                     @tool
management.                             def search(self, query: str) -> list[dict]: ...

                                    $ python -m mcpforge run market:MarketTools
                                    ✓ MCP server running on stdio

为什么选择 mcpforge

模型上下文协议 (MCP) 是 Claude、Cursor 以及一个正在蓬勃发展的 AI 工具生态系统用于调用外部函数的开放标准。它功能强大,但目前编写服务器的过程非常痛苦。

在每个项目中,你都要重复编写相同的 JSON-RPC 框架、模式生成、调度循环和错误处理代码。mcpforge 抹去了这一切。 只需标注你的类,为方法添加类型提示,就完成了。

from mcpforge import serve, tool

@serve(name="market_tools", version="0.1.0")
class MarketTools:
    """Market data tools for AI agents."""

    @tool(description="Get the latest price for a symbol")
    def latest_price(self, symbol: str) -> float:
        return fetch_price(symbol)

    @tool
    def search(self, query: str, limit: int = 10) -> list[dict]:
        """Search ticker symbols matching `query`."""
        return run_search(query, limit)

这是一个完整且符合规范的 MCP 服务器。类型提示会变成 JSON Schema,文档字符串会变成工具描述,返回值会自动序列化。

60 秒快速入门

pip install mcpforge
# hello.py
from mcpforge import serve, tool

@serve(name="hello", version="0.1.0")
class HelloTools:
    @tool
    def greet(self, name: str = "world") -> str:
        """Say hello to someone."""
        return f"Hello, {name}!"
python -m mcpforge run hello:HelloTools

现在你拥有了一个可以通过 stdio 进行 JSON-RPC 2.0 通信的 MCP 服务器。

工作原理

  1. @serve 将一个类标记为 MCP 服务器(名称、版本、功能)。

  2. @tool 将方法注册为 MCP 工具。

  3. @resource(uri=...) 将方法注册为 MCP 资源。

  4. mcpforge 会内省每个方法的签名并生成 JSON Schema —— 原生类型、list[T]dict[str, T]Literal[...]Optional[T]、数据类 (dataclasses) 和 Pydantic 模型均可直接使用。

  5. python -m mcpforge run mod:Class 会启动 stdio 循环,处理 initialize / tools/list / tools/call / resources/list / resources/read,并以正确的代码报告 JSON-RPC 错误。

无需外部 MCP SDK。仅使用标准库 json + pydantic 来实现模式的便捷性。

内置服务器

两个开箱即用的服务器,你可以立即使用:

# Filesystem tools (sandboxed to a root directory)
python -m mcpforge run mcpforge.builtin.filesystem:FilesystemTools

# HTTP fetch tools
python -m mcpforge run mcpforge.builtin.http:HttpTools

FilesystemTools 暴露了 list_dirread_filesearch —— 并带有路径遍历检查的沙箱保护。HttpTools 暴露了带有大小限制的 fetch_url

接入 Claude Desktop

添加到 ~/Library/Application Support/Claude/claude_desktop_config.json

{
  "mcpServers": {
    "market": {
      "command": "python",
      "args": ["-m", "mcpforge", "run", "market:MarketTools"],
      "cwd": "/path/to/your/project"
    }
  }
}

重启 Claude Desktop。你的工具就会出现在对话中。

接入 Cursor

~/.cursor/mcp.json

{
  "mcpServers": {
    "market": {
      "command": "python",
      "args": ["-m", "mcpforge", "run", "market:MarketTools"]
    }
  }
}

对比

特性

mcpforge

mcp SDK

FastMCP

手写实现

单个装饰器

根据类型自动生成 JSON Schema

Pydantic v2 支持

除 pydantic 外无其他依赖

N/A

内置 fs / http 服务器

Hello World 代码行数

~5

~40

~10

~200

检查线路格式

python -m mcpforge inspect market:MarketTools

打印出你的客户端将看到的精确 tools/list 负载。

路线图

  • [x] 工具 (调用, 列表)

  • [x] 资源 (读取, 列表)

  • [x] 类型提示 -> JSON Schema (Pydantic v2, 数据类, Literal, Optional)

  • [x] 内置文件系统和 http 服务器

  • [ ] 资源订阅

  • [ ] 提示词 (Prompts) 功能

  • [ ] 采样 (Sampling) 功能

  • [ ] WebSocket / HTTP-SSE 传输 (可选 [http] 扩展)

  • [ ] 异步工具 (async def)

  • [ ] OTel 追踪钩子

许可证

MIT — 见 LICENSE

作者: thechifuraquantflowstrategos 的姊妹项目。

Install Server
A
license - permissive license
B
quality
C
maintenance

Resources

Unclaimed servers have limited discoverability.

Looking for Admin?

If you are the server author, to access and configure the admin panel.

Latest Blog Posts

MCP directory API

We provide all the information about MCP servers via our MCP API.

curl -X GET 'https://glama.ai/api/mcp/v1/servers/vigilancetrent/mcpforge'

If you have feedback or need assistance with the MCP directory API, please join our Discord server