Skip to content

Python: Skip MCP prompt loading when unsupported#5370

Merged
eavanvalkenburg merged 4 commits into
microsoft:mainfrom
karimbaidar:karimbaidar/issue-5360-mcp-prompts
May 20, 2026
Merged

Python: Skip MCP prompt loading when unsupported#5370
eavanvalkenburg merged 4 commits into
microsoft:mainfrom
karimbaidar:karimbaidar/issue-5360-mcp-prompts

Conversation

@karimbaidar
Copy link
Copy Markdown
Contributor

@karimbaidar karimbaidar commented Apr 20, 2026

Motivation and Context

Fixes #5360.

MCP servers can advertise tools without supporting prompts. The client was still calling prompts/list whenever prompt loading was enabled, so tool-only servers could fail during connect.

Description

Stores the server capabilities returned by MCP initialization and skips prompt discovery when the server does not advertise the prompts capability. If capabilities are unavailable, the existing behavior is preserved.

Adds a focused core MCP regression test for a tool-only server that would otherwise raise Method 'prompts/list' is not available.

Contribution Checklist

  • The code builds clean without any errors or warnings
  • The PR follows the Contribution Guidelines
  • All unit tests pass, and I have added new tests where possible
  • Is this a breaking change? No

Update

This now generalizes the negotiated MCP capability handling for tools, prompts, and logging while keeping the constructor load flags as caller intent. After initialization, missing or unadvertised capabilities are treated as unsupported, so unsupported tools/list, prompts/list, and set_logging_level calls are skipped.

Ping support is handled separately as session-scoped probed state, since it is not part of ServerCapabilities. Paginated tool/prompt loading also reconnects once on ClosedResourceError for servers where ping is unavailable.

@karimbaidar
Copy link
Copy Markdown
Contributor Author

@microsoft-github-policy-service agree

@moonbox3
Copy link
Copy Markdown
Contributor

moonbox3 commented Apr 21, 2026

Python Test Coverage

Python Test Coverage Report •
FileStmtsMissCoverMissing
packages/core/agent_framework
   _mcp.py7525892%153, 172, 317, 377–378, 507, 569, 582, 606–607, 626–629, 631–632, 636, 690–692, 694, 747–749, 804–805, 988, 1025–1026, 1039, 1042, 1050–1051, 1056–1057, 1063, 1107–1108, 1122–1123, 1131–1132, 1137–1138, 1144, 1201, 1204, 1231, 1254–1258, 1363, 1415–1416, 1419, 1890
TOTAL35106410488% 

Python Unit Test Overview

Tests Skipped Failures Errors Time
6959 30 💤 0 ❌ 0 🔥 1m 45s ⏱️

Copy link
Copy Markdown
Contributor

Copilot AI left a comment

Choose a reason for hiding this comment

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

Pull request overview

This PR fixes MCP client connections to tool-only servers by honoring negotiated server capabilities from MCP initialization, avoiding prompt discovery calls when the server does not support prompts.

Changes:

  • Cache MCP initialize() capabilities on MCPTool and use them to detect prompt support.
  • Skip prompts/list discovery when the server does not advertise the prompts capability (preserving existing behavior when capabilities are unavailable).
  • Add a regression test ensuring prompt discovery is skipped for tool-only servers.

Reviewed changes

Copilot reviewed 2 out of 2 changed files in this pull request and generated no comments.

File Description
python/packages/core/agent_framework/_mcp.py Store negotiated server capabilities and gate prompt loading on prompts capability support.
python/packages/core/tests/core/test_mcp.py Add regression coverage for connecting to a server that supports tools but not prompts.

Comment thread python/packages/core/agent_framework/_mcp.py Outdated
@karimbaidar karimbaidar force-pushed the karimbaidar/issue-5360-mcp-prompts branch from b621758 to 629d5a4 Compare May 12, 2026 19:29
Copy link
Copy Markdown
Contributor

@giles17 giles17 left a comment

Choose a reason for hiding this comment

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

Automated Code Review

Reviewers: 4 | Confidence: 89% | Result: All clear

Reviewed: Correctness, Security Reliability, Test Coverage, Design Approach


Automated review by giles17's agents

@giles17
Copy link
Copy Markdown
Contributor

giles17 commented May 12, 2026

@karimbaidar looks good. One thing to flag:
When _ping_available is False, _ensure_connected() returns immediately without any liveness check. That's fine for call_tool()/get_prompt() since they have their own ClosedResourceError retry loops but load_tools() and load_prompts() don't. They rely entirely on _ensure_connected() for reconnection during pagination. So if the transport drops on a ping-less server, list_tools()/list_prompts() will raise an unhandled ClosedResourceError.

Comment thread python/packages/core/agent_framework/_mcp.py Outdated
Comment thread python/packages/core/agent_framework/_mcp.py
Comment thread python/packages/core/agent_framework/_mcp.py Outdated
@karimbaidar karimbaidar force-pushed the karimbaidar/issue-5360-mcp-prompts branch from 629d5a4 to b44b49e Compare May 13, 2026 06:19
@karimbaidar
Copy link
Copy Markdown
Contributor Author

karimbaidar commented May 13, 2026

Thanks for the review @giles17. I just Removed the constant, added _supports_logging, made missing capabilities unsupported after init, and added reconnect handling for ping-less paginated loads. Could you please take another look?

@karimbaidar
Copy link
Copy Markdown
Contributor Author

Hi @giles17 i fixed the pyright checks. If possible, can you please approve this PR ? Thank you!

@giles17
Copy link
Copy Markdown
Contributor

giles17 commented May 15, 2026

Hey @karimbaidar thanks! There's a little merge conflict to be resolved

…360-mcp-prompts

# Conflicts:
#	python/packages/core/agent_framework/_mcp.py
@karimbaidar
Copy link
Copy Markdown
Contributor Author

@giles17 thanks! just resolved the merge conflict but I think github requires your approval again :)

@karimbaidar
Copy link
Copy Markdown
Contributor Author

@eavanvalkenburg can you please also check and approve so that this PR could be merged successfully. Because we need at least two approvers for this PR to be merged successfully. thank you!

@giles17 giles17 requested a review from eavanvalkenburg May 18, 2026 15:27
Copy link
Copy Markdown
Member

@eavanvalkenburg eavanvalkenburg left a comment

Choose a reason for hiding this comment

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

small nit, otherwise looks good

Comment thread python/packages/core/agent_framework/_mcp.py Outdated
@karimbaidar
Copy link
Copy Markdown
Contributor Author

karimbaidar commented May 20, 2026

@giles17 @eavanvalkenburg all Set :), waiting for the final Approval and thanks a lot for your review. It helped a lot to improve this PR further. Truly appreciate that.

@eavanvalkenburg eavanvalkenburg enabled auto-merge May 20, 2026 11:36
@eavanvalkenburg eavanvalkenburg added this pull request to the merge queue May 20, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Python: [Bug]: Found capabilities during initialize are not respected and is throwing exception

5 participants