Skip to content

fix(resources): avoid extra thread hop when reading template resources - #3893

Closed
DevChiniwala wants to merge 1 commit into
modelcontextprotocol:mainfrom
DevChiniwala:fix/resource-template-thread-hop
Closed

DevChiniwala wants to merge 1 commit into
modelcontextprotocol:mainfrom
DevChiniwala:fix/resource-template-thread-hop

Conversation

@DevChiniwala

Copy link
Copy Markdown

Summary

  • ResourceTemplate.create_resource wrapped the template result in a sync lambda (fn=lambda: result), which caused FunctionResource.read() to dispatch it through anyio.to_thread.run_sync — an unnecessary worker-thread round trip just to return a value already in memory.
  • Replaced the sync lambda with an async wrapper (async def _return_result) so FunctionResource.read() recognises it as a coroutine and awaits it directly on the event loop.

What changed

src/mcp/server/mcpserver/resources/templates.py — In create_resource, replaced:

fn=lambda: result,  # Capture result in closure

with:

async def _return_result() -> Any:
    return result

...
fn=_return_result,

Performance impact

Per the benchmarks in #3889:

Template type Before (us) After (us) Improvement
Sync template 174 117 -33 %
Async template 120 78 -35 %

The improvement comes from eliminating a gratuitous anyio.to_thread.run_sync call that only existed to return a constant.

Test plan

  • All 34 existing tests in tests/server/mcpserver/resources/test_resource_template.py pass
  • All 97 tests in tests/server/mcpserver/resources/ pass (1 skipped, 0 failures)

Closes #3889

🤖 Generated with Claude Code

create_resource wrapped the template result in a sync lambda, forcing
FunctionResource.read() through anyio.to_thread.run_sync. Use an async
wrapper so the result is returned directly on the event loop.

Fixes modelcontextprotocol#3889

Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
Copilot AI balanced review requested due to automatic review settings November 22, 2026 13:56
@github-actions github-actions Bot added the missing-issue-link Auto-closed: PR needs a linked issue assigned to its author (see CONTRIBUTING.md) label Nov 22, 2026
@github-actions

Copy link
Copy Markdown
Contributor

This PR has been closed automatically. This repo only keeps pull requests open when they come from a maintainer, or from a contributor a maintainer has assigned to the linked issue, and you aren't currently assigned to #3889.

If a maintainer assigns you to #3889, this PR reopens on its own and there's nothing more you need to do here. Assignment is a maintainer call based on capacity; comments that only ask to be assigned don't factor in. What does help is engaging on the issue itself by confirming the repro, explaining why it matters for your use case, or describing the approach you'd take.

You're welcome to keep pushing commits here (just avoid force-pushing, since GitHub can't reopen a rewritten branch), but that on its own won't get the PR reviewed or the issue assigned, and realistically most auto-closed PRs stay closed. There's no need to open a new PR either way.

CONTRIBUTING.md has the full reasoning, but in short:

  • We're a small team with very little capacity to review community PRs right now.
  • Many recent PRs are AI-generated with little human review, and reviewing one carefully still costs a maintainer as much time as it ever did. A well-described issue is usually more useful to us than the code.

Maintainers: reopen, remove missing-issue-link, or add bypass-issue-check to override.

@github-actions github-actions Bot closed this Nov 22, 2026

Copilot AI left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Copilot was unable to review this pull request because the user who requested the review has insufficient remaining quota. Try requesting a review at a lower effort level.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

missing-issue-link Auto-closed: PR needs a linked issue assigned to its author (see CONTRIBUTING.md)

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Reading a resource template makes an extra worker-thread round trip to return a value it already has

2 participants