重要前提
安装AI Skills的关键前提是:必须科学上网,且开启TUN模式,这一点至关重要,直接决定安装能否顺利完成,在此郑重提醒三遍:科学上网,科学上网,科学上网。查看完整安装教程 →
fastmcp-server by davila7/claude-code-templates
npx skills add https://github.com/davila7/claude-code-templates --skill fastmcp-server使用 FastMCP 3.0 构建生产就绪的 MCP(模型上下文协议)服务器的完整参考指南 - 这是一个快速、Pythonic 的框架,用于将大语言模型连接到工具和数据。
在以下情况下使用 FastMCP 服务器:
涵盖的关键领域:
创建带有工具的服务器:
from fastmcp import FastMCP
mcp = FastMCP("MyServer")
@mcp.tool
def add(a: int, b: int) -> int:
"""Add two numbers"""
return a + b
广告位招租
在这里展示您的产品或服务
触达数万 AI 开发者,精准高效
@mcp.resource("data://config")
def get_config() -> dict:
"""Return server configuration"""
return {"version": "1.0", "debug": False}
创建资源模板:
@mcp.resource("users://{user_id}/profile")
def get_user_profile(user_id: str) -> dict:
"""Get a user's profile by ID"""
return fetch_user(user_id)
创建提示:
@mcp.prompt
def review_code(code: str, language: str = "python") -> str:
"""Review code for best practices"""
return f"Review this {language} code:\n\n{code}"
运行服务器:
if __name__ == "__main__":
mcp.run()
# Or with transport options:
# mcp.run(transport="sse", host="0.0.0.0", port=8000)
from fastmcp import FastMCP, Context
mcp = FastMCP("MyServer")
@mcp.tool
def process_data(uri: str, ctx: Context) -> str:
"""Process data with logging and progress"""
ctx.info(f"Processing {uri}")
ctx.report_progress(0, 100)
data = ctx.read_resource(uri)
ctx.report_progress(100, 100)
return f"Processed: {data}"
from fastmcp import FastMCP
from fastmcp.server.auth import BearerAuthProvider
auth = BearerAuthProvider(
jwks_uri="https://your-provider/.well-known/jwks.json",
audience="your-api",
issuer="https://your-provider/"
)
mcp = FastMCP("SecureServer", auth=auth)
作为可执行能力暴露给大语言模型的函数。使用 @mcp.tool 装饰。支持 Pydantic 验证、异步、自定义返回类型和注解(readOnlyHint、destructiveHint)。
由 URI 标识的静态或动态数据源。资源使用固定 URI(data://config),模板使用参数化 URI(users://{id}/profile)。支持 MIME 类型、注解和通配符参数。
Context 对象提供在工具/资源内部访问 MCP 功能的能力:日志记录、进度报告、资源访问、大语言模型采样、用户引导和会话状态。
使用 Depends() 将值注入到工具/资源函数中。支持 HTTP 请求、访问令牌、自定义依赖项和基于生成器的清理模式。
控制组件来源。LocalProvider(默认,基于装饰器)、FileSystemProvider(从磁盘上的 Python 文件加载)、SkillsProvider(打包的捆绑包)或自定义提供程序。
多种身份验证模式:令牌验证(JWT、JWKS)、OAuth 代理、OIDC 代理、远程 OAuth 和完整的 OAuth 服务器。通过组件上的作用域和中间件进行授权。
拦截和修改请求/响应。内置中间件用于速率限制、错误处理、日志记录和响应大小限制。通过 @mcp.middleware 实现自定义中间件。
详细文档组织在 references/ 文件夹中:
v1.0.0(2026年2月)
每周安装量
42
代码仓库
GitHub 星标数
22.6K
首次出现
2026年2月17日
安全审计
安装于
codex42
opencode40
gemini-cli40
github-copilot40
amp40
kimi-cli40
Complete reference for building production-ready MCP (Model Context Protocol) servers with FastMCP 3.0 - the fast, Pythonic framework for connecting LLMs to tools and data.
Use FastMCP Server when:
Key areas covered:
Create a server with tools:
from fastmcp import FastMCP
mcp = FastMCP("MyServer")
@mcp.tool
def add(a: int, b: int) -> int:
"""Add two numbers"""
return a + b
Create a resource:
@mcp.resource("data://config")
def get_config() -> dict:
"""Return server configuration"""
return {"version": "1.0", "debug": False}
Create a resource template:
@mcp.resource("users://{user_id}/profile")
def get_user_profile(user_id: str) -> dict:
"""Get a user's profile by ID"""
return fetch_user(user_id)
Create a prompt:
@mcp.prompt
def review_code(code: str, language: str = "python") -> str:
"""Review code for best practices"""
return f"Review this {language} code:\n\n{code}"
Run the server:
if __name__ == "__main__":
mcp.run()
# Or with transport options:
# mcp.run(transport="sse", host="0.0.0.0", port=8000)
from fastmcp import FastMCP, Context
mcp = FastMCP("MyServer")
@mcp.tool
def process_data(uri: str, ctx: Context) -> str:
"""Process data with logging and progress"""
ctx.info(f"Processing {uri}")
ctx.report_progress(0, 100)
data = ctx.read_resource(uri)
ctx.report_progress(100, 100)
return f"Processed: {data}"
from fastmcp import FastMCP
from fastmcp.server.auth import BearerAuthProvider
auth = BearerAuthProvider(
jwks_uri="https://your-provider/.well-known/jwks.json",
audience="your-api",
issuer="https://your-provider/"
)
mcp = FastMCP("SecureServer", auth=auth)
Functions exposed as executable capabilities for LLMs. Decorated with @mcp.tool. Support Pydantic validation, async, custom return types, and annotations (readOnlyHint, destructiveHint).
Static or dynamic data sources identified by URIs. Resources use fixed URIs (data://config), templates use parameterized URIs (users://{id}/profile). Support MIME types, annotations, and wildcard parameters.
The Context object provides access to MCP features within tools/resources: logging, progress reporting, resource access, LLM sampling, user elicitation, and session state.
Inject values into tool/resource functions using Depends(). Supports HTTP requests, access tokens, custom dependencies, and generator-based cleanup patterns.
Control where components come from. LocalProvider (default, decorator-based), FileSystemProvider (load from Python files on disk), SkillsProvider (packaged bundles), or custom providers.
Multiple auth patterns: token verification (JWT, JWKS), OAuth proxy, OIDC proxy, remote OAuth, and full OAuth server. Authorization via scopes on components and middleware.
Intercept and modify requests/responses. Built-in middleware for rate limiting, error handling, logging, and response size limits. Custom middleware via @mcp.middleware.
Detailed documentation is organized in the references/ folder:
v1.0.0 (February 2026)
Weekly Installs
42
Repository
GitHub Stars
22.6K
First Seen
Feb 17, 2026
Security Audits
Gen Agent Trust HubPassSocketPassSnykWarn
Installed on
codex42
opencode40
gemini-cli40
github-copilot40
amp40
kimi-cli40
SoulTrace 人格评估 API - 基于五色心理模型的贝叶斯自适应测试
56,700 周安装