Skip to main content
Glama
hussain-alsaibai

tiny-mcp-server

tiny-mcp-server

在 Python 中构建 Model Context Protocol (MCP) 服务器的最快、最小巧的方式。零依赖。MIT.

tiny-mcp-server 是一个用于构建 Model Context Protocol 服务器的单文件 Python 框架。无外部依赖,无样板代码 —— 只需用 @tool 装饰一个函数,你就在交付一个 MCP 服务器。

功能

  • 🪶 单文件、零依赖 —— 纯 Python 标准库,可放入任何项目

  • 🪄 @tool 装饰器 —— 用一行代码将任意函数变为 MCP 工具

  • 🚀 多种传输方式 —— stdio(用于桌面 MCP 客户端)和 sse(用于 Web/远程)

  • � 自动生成 JSON Schema —— 类型提示和 docstrings 变成工具 schema

  • �️ 内置日志和错误处理 —— 永远不会因格式错误的负载而崩溃

  • �️ MIT 许可 —— 可在任何地方使用

Related MCP server: Berry MCP Server

安装

选择最方便的方式:

# Option 1 — pip (when published)
pip install tiny-mcp-server

# Option 2 — curl a single file and import it
curl -O https://raw.githubusercontent.com/hussain-alsaibai/tiny-mcp-server/main/tiny_mcp_server.py

# Option 3 — vendor it
cp tiny_mcp_server.py your_project/

快速开始

这是一个包含两个工具的完整 MCP 服务器 —— 不到 20 行:

from tiny_mcp_server import Server, tool

app = Server("hello-world")

@app.tool
def add(a: int, b: int) -> int:
    """Add two numbers together."""
    return a + b

@app.tool
def greet(name: str = "World") -> str:
    """Greet someone by name."""
    return f"Hello, {name}!"

if __name__ == "__main__":
    app.run("stdio")  # or "sse" for HTTP transport

add 工具自动生成的 JSON Schema:

{
  "name": "add",
  "description": "Add two numbers together.",
  "inputSchema": {
    "type": "object",
    "properties": {
      "a": {"type": "integer"},
      "b": {"type": "integer"}
    },
    "required": ["a", "b"]
  }
}

传输方式

stdio(默认 —— 用于 Claude Desktop、Cursor 等)

app.run("stdio")

服务器从 stdin 读取 JSON-RPC 消息,并将响应写入 stdout。这是每个桌面 MCP 客户端使用的传输方式。

SSE(HTTP —— 用于 Web 客户端)

app.run("sse", host="127.0.0.1", port=8000)

在 /sse 暴露一个 Server-Sent Events 端点,并在 /messages 暴露一个 POST 端点。非常适合将 MCP 服务器作为本地微服务运行。

API 参考

Server(name: str)

app = Server("my-server")

@app.tool

@app.tool(name="optional-name", description="optional-desc")
def my_tool(arg: str, count: int = 1) -> str:
    """Tool description (used as fallback if description not provided)."""
    return arg * count
  • name —— 默认为函数名

  • description —— 默认为函数的 docstring

  • 参数从签名中推断;类型来自注解

  • 默认值在 schema 中变为可选

app.run(transport: str, **kwargs)

  • "stdio" —— 无额外参数

  • "sse" —— host(默认 127.0.0.1)、port(默认 8000)

示例

可运行的演示请参阅 examples/hello_world.py。

许可证

MIT © hussain-alsaibai

Related MCP Connectors

Related MCP Servers

  • A
    license
    D
    quality
    D
    maintenance
    A foundation for building custom local Model Context Protocol (MCP) servers that provide tools accessible to AI assistants like Cursor or Claude Desktop.
    1
    37
    MIT
  • A
    license
    Not graded
    quality
    D
    maintenance
    A universal framework for creating and deploying custom Model Context Protocol (MCP) tool servers with decorator-based tool registration, supporting multiple transports and automatic JSON schema generation for AI assistants.
    1
    MIT
  • A
    license
    Not graded
    quality
    D
    maintenance
    A simple toolkit for creating MCP servers with stdio and SSE transport, auto-validating tools via Pydantic.
    MIT