MCP Python SDK教程:深入理解FastMCP Context机制

MCP Python SDK教程:深入理解FastMCP Context机制

Tutorial-Codebase-Knowledge Turns Codebase into Easy Tutorial with AI Tutorial-Codebase-Knowledge 项目地址: https://gitcode.com/gh_mirrors/tu/Tutorial-Codebase-Knowledge

前言

在构建AI应用服务时,一个常见的需求是让长时间运行的任务能够实时反馈执行状态,或者让工具函数能够访问服务端的共享资源。MCP Python SDK中的FastMCP框架通过Context机制优雅地解决了这些问题。本文将深入解析这一核心机制,帮助开发者掌握如何利用Context构建更强大的AI服务。

Context机制概述

Context是FastMCP框架中一个强大的上下文对象,它为工具函数和资源函数提供了请求级别的上下文环境。简单来说,Context就像是一个"后台通行证",允许你的函数在执行过程中:

  1. 向客户端发送实时日志信息
  2. 报告任务执行进度
  3. 访问服务端定义的其他资源
  4. 获取当前请求的元数据

核心功能解析

1. 日志反馈机制

Context提供了多级日志反馈接口:

await ctx.debug("调试信息")  # 调试级别日志
await ctx.info("普通信息")   # 信息级别日志
await ctx.warning("警告信息") # 警告级别日志
await ctx.error("错误信息")   # 错误级别日志

这些日志会实时发送到客户端,非常适合用于:

  • 记录任务执行的关键步骤
  • 报告异常情况
  • 提供调试信息

2. 进度报告功能

对于长时间运行的任务,进度反馈至关重要:

await ctx.report_progress(current_step, total_steps)

这个功能可以:

  • 让客户端显示进度条
  • 预估剩余时间
  • 提供任务取消的依据

3. 资源访问能力

Context允许工具函数访问服务端定义的其他资源:

resource_contents = await ctx.read_resource("config://task_settings")

这种设计实现了:

  • 松耦合的模块设计
  • 动态配置加载
  • 资源共享与复用

实战示例

基础示例:带进度报告的任务

import anyio
from mcp.server.fastmcp import FastMCP
from mcp.server.fastmcp.server import Context

server = FastMCP(name="TaskServer")

@server.tool(name="long_task")
async def run_long_task(duration: int, ctx: Context) -> str:
    await ctx.info(f"任务开始,预计耗时{duration}秒")
    
    steps = 5
    for i in range(steps):
        await ctx.debug(f"正在处理第{i+1}/{steps}步")
        await anyio.sleep(duration/steps)
        await ctx.report_progress(i+1, steps)
    
    await ctx.info("任务完成!")
    return "执行成功"

进阶示例:结合资源访问

@server.resource(uri="config://settings")
def get_settings() -> dict:
    return {"mode": "fast", "retry": 3}

@server.tool(name="enhanced_task")
async def enhanced_task(ctx: Context) -> str:
    settings = await ctx.read_resource("config://settings")
    await ctx.info(f"当前配置:{settings}")
    
    if settings.get("mode") == "fast":
        await ctx.debug("快速模式执行中...")
    else:
        await ctx.debug("标准模式执行中...")
    
    return "任务完成"

实现原理深度解析

1. 上下文生命周期

  1. 请求到达:客户端发起请求
  2. 上下文创建:FastMCP创建底层RequestContext
  3. 包装处理:生成高级Context对象
  4. 函数调用:通过工具管理器调用目标函数
  5. 参数注入:自动注入Context对象
  6. 执行反馈:通过Context与客户端交互

2. 关键技术点

  • 类型提示(Type Hint):通过ctx: Context参数标记实现自动注入
  • 请求隔离:每个请求拥有独立的Context实例
  • 会话管理:通过底层ServerSession实现双向通信

3. 核心类结构

classDiagram
    class Context {
        +_request_context: RequestContext
        +_fastmcp: FastMCP
        +info(message: str)
        +debug(message: str)
        +warning(message: str)
        +error(message: str)
        +report_progress(current, total)
        +read_resource(uri)
    }
    
    class FastMCP {
        +get_context() Context
        +read_resource(uri)
    }
    
    class RequestContext {
        +session: ServerSession
        +meta: RequestMeta
    }
    
    Context --> FastMCP
    Context --> RequestContext

最佳实践

  1. 命名规范:建议使用ctx作为参数名,保持代码一致性
  2. 错误处理:对资源访问操作添加try-catch块
  3. 性能考虑:避免在循环中发送过多日志
  4. 安全注意:敏感信息不应通过日志暴露

常见问题解答

Q: Context对象是线程安全的吗?

A: 是的,每个请求都有独立的Context实例,天然支持并发。

Q: 能否在资源函数中使用Context?

A: 可以,资源函数同样支持Context注入。

Q: 如何测试带Context的工具函数?

A: 可以mock一个Context对象进行单元测试。

总结

FastMCP的Context机制为AI服务开发提供了强大的交互能力,使得工具函数不再是孤立的执行单元,而是能够:

  1. 实时反馈执行状态
  2. 动态获取配置信息
  3. 与其他服务组件交互
  4. 保持请求级别的隔离性

掌握Context的使用,将显著提升你开发的AI服务的交互性和可用性。在实际项目中,合理利用Context机制可以构建出更加健壮、用户友好的AI应用服务。

Tutorial-Codebase-Knowledge Turns Codebase into Easy Tutorial with AI Tutorial-Codebase-Knowledge 项目地址: https://gitcode.com/gh_mirrors/tu/Tutorial-Codebase-Knowledge

创作声明:本文部分内容由AI辅助生成(AIGC),仅供参考

### MCP in Python Usage and Implementation #### Overview of MCP in Python The Model Context Protocol (MCP) is a protocol designed to facilitate interactions between AI models and external tools, data sources, or APIs[^3]. In the context of Python, MCP can be implemented using the MCP Python SDK, which provides tools for building both servers and clients. This implementation allows developers to interact with MCP bridges or agents effectively. #### Installing MCP Python SDK To integrate MCP into Python projects, the MCP Python SDK can be installed via pip: ```bash pip install mcp ``` This command installs the necessary libraries for interacting with MCP servers or clients[^1]. #### Configuring MCP Server in Python A MCP server can be configured in Python by defining its behavior and endpoints. Below is an example of setting up a basic MCP server using Python: ```python from mcp.server import MCPServer def handle_request(data): # Process incoming request data return {"result": "Processed"} if __name__ == "__main__": server = MCPServer(handle_request, port=8080) server.start() ``` In this example, the `MCPServer` class initializes a server that listens on port 8080 and processes incoming requests by calling the `handle_request` function[^1]. #### Configuring MCP Client in Python For interacting with an existing MCP server, a client can be set up as follows: ```python from mcp.client import MCPClient client = MCPClient(mcp_url="http://localhost:8080", mcp_port=8080) response = client.send_request({"action": "fetch_data"}) print(response) ``` Here, the `MCPClient` sends a request to the MCP server at the specified URL and port, and retrieves the response[^2]. #### Advanced Configuration Options MCP servers and clients can be further customized with additional parameters such as JSON formatting, logging levels, and security settings. For instance: ```python client = MCPClient( mcp_url="http://localhost:8080", mcp_port=8080, hide_json=True, json_width=120 ) ``` This configuration hides JSON results from tool executions and sets the maximum width for JSON output to 120 characters. #### Integration with Databases MCP can also be integrated with databases to enhance data retrieval and model interaction. This approach offers advantages over traditional RAG methods by providing more efficient and precise data access[^4]. An example of integrating MCP with a database might look like this: ```python from mcp.server import MCPServer import sqlite3 def fetch_data_from_db(query): conn = sqlite3.connect("example.db") cursor = conn.cursor() cursor.execute(query) result = cursor.fetchall() conn.close() return result def handle_request(data): query = data.get("query") if query: return {"data": fetch_data_from_db(query)} return {"error": "No query provided"} if __name__ == "__main__": server = MCPServer(handle_request, port=8080) server.start() ``` This script sets up an MCP server that executes SQL queries against a SQLite database[^4]. #### Best Practices for MCP Implementation - Ensure secure communication between MCP clients and servers using authentication mechanisms. - Optimize performance by configuring appropriate logging levels and resource limits. - Test the MCP implementation thoroughly to handle edge cases and errors gracefully.
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

花淑云Nell

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值