mcpforge
mcpforge
用 5 行 Python 代码发布一个 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,文档字符串会变成工具描述,返回值会自动序列化。
Related MCP server: nbmcp
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 服务器。
工作原理
@serve将一个类标记为 MCP 服务器(名称、版本、功能)。@tool将方法注册为 MCP 工具。@resource(uri=...)将方法注册为 MCP 资源。mcpforge 会内省每个方法的签名并生成 JSON Schema —— 原生类型、
list[T]、dict[str, T]、Literal[...]、Optional[T]、数据类 (dataclasses) 和 Pydantic 模型均可直接使用。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:HttpToolsFilesystemTools 暴露了 list_dir、read_file、search —— 并带有路径遍历检查的沙箱保护。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 |
| 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。
作者: thechifura。quantflow 和 strategos 的姊妹项目。
Available Tools
3 toolslist_dirA
List entries in path (relative to the sandbox root).
| Name | Required | Description | Default |
|---|---|---|---|
| path | No | . |
TDQS
Does the description disclose side effects, auth requirements, rate limits, or destructive behavior?
No annotations provided, so description bears full burden. It does not disclose what types of entries are listed (files, directories), sorting order, or any other behavioral traits beyond listing.
Agents need to know what a tool does to the world before calling it. Descriptions should go beyond structured annotations to explain consequences.
Is the description appropriately sized, front-loaded, and free of redundancy?
Single sentence with no wasted words. Verb is front-loaded, and the essential context is included efficiently.
Shorter descriptions cost fewer tokens and are easier for agents to parse. Every sentence should earn its place.
Given the tool's complexity, does the description cover enough for an agent to succeed on first attempt?
For a simple listing tool with one parameter and no output schema, the description is adequate but could be more complete by noting whether entries include both files and directories.
Complex tools with many parameters or behaviors need more documentation. Simple tools need less. This dimension scales expectations accordingly.
Does the description clarify parameter syntax, constraints, interactions, or defaults beyond what the schema provides?
Input schema has 0% description coverage, but description adds meaning by specifying that path is relative to the sandbox root. This clarifies a key detail not in the schema.
Input schemas describe structure but not intent. Descriptions should explain non-obvious parameter relationships and valid value ranges.
Does the description clearly state what the tool does and how it differs from similar tools?
Description clearly states the verb 'list', the resource 'entries in path', and adds context 'relative to the sandbox root'. It distinguishes from sibling tools read_file and search.
Agents choose between tools based on descriptions. A clear purpose with a specific verb and resource helps agents select the right tool.
Does the description explain when to use this tool, when not to, or what alternatives exist?
No guidance on when to use this tool versus alternatives or exclusions. The description does not mention any context or conditions for usage.
Agents often have multiple tools that could apply. Explicit usage guidance like "use X instead of Y when Z" prevents misuse.
read_fileA
Read up to max_bytes bytes of the file at path as UTF-8 text.
| Name | Required | Description | Default |
|---|---|---|---|
| path | Yes | ||
| max_bytes | No |
TDQS
Does the description disclose side effects, auth requirements, rate limits, or destructive behavior?
The description discloses that it reads up to max_bytes as UTF-8 text, but does not mention error behavior (e.g., file not found, binary files) or performance implications. With no annotations, the description provides moderate transparency.
Agents need to know what a tool does to the world before calling it. Descriptions should go beyond structured annotations to explain consequences.
Is the description appropriately sized, front-loaded, and free of redundancy?
The description is a single, clear sentence that efficiently covers the tool's core functionality without extraneous words.
Shorter descriptions cost fewer tokens and are easier for agents to parse. Every sentence should earn its place.
Given the tool's complexity, does the description cover enough for an agent to succeed on first attempt?
Given no output schema and sibling tools, the description could mention return value format or error handling. It adequately covers the main behavior but lacks completeness for a file reading tool.
Complex tools with many parameters or behaviors need more documentation. Simple tools need less. This dimension scales expectations accordingly.
Does the description clarify parameter syntax, constraints, interactions, or defaults beyond what the schema provides?
Schema description coverage is 0%, so the description must compensate. It explains both parameters: path is the file location, max_bytes limits the bytes read, and specifies UTF-8 encoding. However, it does not clarify path format (absolute/relative).
Input schemas describe structure but not intent. Descriptions should explain non-obvious parameter relationships and valid value ranges.
Does the description clearly state what the tool does and how it differs from similar tools?
The description clearly states it reads a file at a given path, up to a specified number of bytes, as UTF-8 text. It distinguishes from sibling tools 'list_dir' (listing directory contents) and 'search' (searching for files).
Agents choose between tools based on descriptions. A clear purpose with a specific verb and resource helps agents select the right tool.
Does the description explain when to use this tool, when not to, or what alternatives exist?
No guidance is provided on when to use this tool versus alternatives. There is no mention of when not to use it or any prerequisites.
Agents often have multiple tools that could apply. Explicit usage guidance like "use X instead of Y when Z" prevents misuse.
searchC
Recursively search for pattern in files under path.
| Name | Required | Description | Default |
|---|---|---|---|
| pattern | Yes | ||
| path | No | . | |
| max_results | No |
TDQS
Does the description disclose side effects, auth requirements, rate limits, or destructive behavior?
Without annotations, the description must disclose behavior fully. It mentions recursion but omits critical details: pattern syntax (regex/glob?), case sensitivity, file inclusion criteria, effect of max_results, and whether any side effects occur. This is insufficient for an agent to predict tool behavior.
Agents need to know what a tool does to the world before calling it. Descriptions should go beyond structured annotations to explain consequences.
Is the description appropriately sized, front-loaded, and free of redundancy?
The description is a single sentence, making it concise, but it omits necessary details, so it is under-specified rather than efficiently compact. It could be longer to include essential context without becoming wordy.
Shorter descriptions cost fewer tokens and are easier for agents to parse. Every sentence should earn its place.
Given the tool's complexity, does the description cover enough for an agent to succeed on first attempt?
For a tool with 3 parameters, no output schema, and no annotations, the description must explain return values, error handling, and behavior under limits. It does not, leaving major gaps for an agent using this tool.
Complex tools with many parameters or behaviors need more documentation. Simple tools need less. This dimension scales expectations accordingly.
Does the description clarify parameter syntax, constraints, interactions, or defaults beyond what the schema provides?
Schema description coverage is 0%, so the description should compensate. It mentions pattern and path but does not define pattern semantics or default behavior for path. max_results is not explained at all. The description adds minimal value beyond parameter names.
Input schemas describe structure but not intent. Descriptions should explain non-obvious parameter relationships and valid value ranges.
Does the description clearly state what the tool does and how it differs from similar tools?
The description clearly states the tool recursively searches for a pattern in files under a path. It uses a specific verb ('search') and resource ('files'), and differentiates from sibling tools like list_dir and read_file, which serve different purposes.
Agents choose between tools based on descriptions. A clear purpose with a specific verb and resource helps agents select the right tool.
Does the description explain when to use this tool, when not to, or what alternatives exist?
No guidance on when to use this tool versus alternatives or when not to use it. The description lacks context about prerequisites, such as file types or permissions, and does not mention any alternative search methods.
Agents often have multiple tools that could apply. Explicit usage guidance like "use X instead of Y when Z" prevents misuse.
Tool Schema Changelog
Recent tool additions, removals, and schema changes observed during successful MCP inspections.
3 tool updates
v0.1.0- First observed
list_dir - First observed
read_file - First observed
search
TDQS
Scored across 3 tools
Each tool has a clearly distinct purpose: listing directory entries, reading file contents, and searching for patterns. No overlap in functionality.
Naming is mainly verb_noun (list_dir, read_file) with one tool using a single verb (search). All use consistent snake_case, but slight pattern deviation prevents a perfect score.
Three tools is a reasonable and focused set for basic file operations within a sandbox. Each tool serves a distinct and necessary function.
Only read-only operations are provided. Missing write, update, or delete tools, which are essential for a file manipulation server, leaving significant gaps.
Maintenance
Related MCP Connectors
A simple MCP server built with FastMCP and python
An MCP server that let you interact with Cycloid.io Internal Development Portal and Platform
MCP server for progressive tool usage at any scale (see https://klavis.ai)
Nifty's MCP server — exposes tasks, projects, messages, and files as tools for AI agents.
Related MCP Servers
- FlicenseNot gradedqualityDmaintenanceA MCP server framework with zero-config auto-discovery, type-safe decorators, and HTTP transport, enabling easy creation and deployment of tools, prompts, and resources to LeanMCP Cloud.-
- AlicenseNot gradedqualityDmaintenanceEnables building lightweight MCP servers where Rust handles transport, routing, and schema validation while Python defines tool behavior using type hints, with support for stdio and HTTP transports.MIT
- AlicenseNot gradedqualityBmaintenanceA FastAPI-style framework for building MCP servers in Python, enabling declarative tool definitions with automatic schema validation and multiple transport options.MIT
- AlicenseNot gradedqualityBmaintenanceA no-nonsense framework for building stdio MCP servers using plain async handlers and type annotations, with strict schema validation and no configuration overhead.MIT