modelcontextprotocol / modelcontextprotocol/python-sdk
How can I manage resource across multiple tool calls during one session?
未关闭
还没有人认领这个 Issue。
- 主要语言
- Python
- 星标
- 24.3k
- 派生
- 4k
- 平均合并
- 1 天 1 小时
- 30 天内合并 PR
- 31
描述
Description
I was messing around multiple agents running in parallel, and suffered from some kind of race condition. I had to write a lock for them.
It looks like each agent has their own session, but I don't have any idea to handle how they create and terminate.
The code can be like below, assuming it is a streamable-http app:
import asyncio
import mcp
from mcp.server.fastmthemcp import Context
import mcp.events # some mcp protocol events, maybe session creation or termination.
mcp_server = mcp.server.FastMCP("demo-locking")
lock = None
async def acquire_lock(ctx: Context):
# assume ctx.session.session_id = mcp-session-id acquired from headers, however not defined in this field.
while lock is not None and lock != ctx.session.session_id:
await asyncio.sleep(1)
ctx.info("Waiting for the lock")
lock = ctx.session.session_id
@mcp_server.tool()
async def tool1(ctx: Context):
await acquire_lock(ctx)
# ... do tool things
@mcp_server.tool()
async def tool2(ctx: Context):
await acquire_lock(ctx)
# ... do tool things
async def release_lock(ctx: Context):
if lock == ctx.session.session_id:
lock = None
mcp_server.add_handler(mcp.events.SESSION_TERMINATE, release_lock) # should be something like this to register a session cleanup.
app = mcp_server.streamable_http_app()
import uvicorn
uvicorn.run(app, host="0.0.0.0", port=8000)
References
No response
贡献指南
从这里开始
- 先读完整个 Issue,再读项目的贡献指南。
- 在 Issue 下留言说明你要接手 —— 这能避免两个人做同样的事。
- Fork 仓库,在一个分支上完成修改。
- 提交 Pull Request,并在描述里引用这个 Issue 编号。
调研方向
首先阅读 streamable_http_app() 入口点以及示例中的 Context/session 引用,然后检查 add_handler() 和 SESSION_TERMINATE 当前是如何定义的。该 issue 未指出现有的文件或测试。要视为完成,需要确定并记录一种协调工具调用之间资源以及处理会话清理的方法。
由索引模型根据 Issue 内容生成。
评估
- 技术栈
- python
- 领域
- backend-api-design
- Issue 类型
- 功能
- 难度
- 5/5
- 预计耗时
- 一周以上
- 活跃度
- 活跃
- 描述清晰度
- 需要澄清
- 新手友好度
- 35/100