Skip to content

Python: Replace MCP prompt catalog on refresh - #9121

Merged
Eduard van Valkenburg (eavanvalkenburg) merged 3 commits into
microsoft:mainfrom
Anshuman-Prakash:fix/mcp-prompt-refresh-9115
Oct 13, 2026
Merged

Eduard van Valkenburg (eavanvalkenburg) merged 3 commits into
microsoft:mainfrom
Anshuman-Prakash:fix/mcp-prompt-refresh-9115

Conversation

@Anshuman-Prakash

Copy link
Copy Markdown
Contributor

Motivation & Context

MCPTool._load_prompts_locked refreshed prompts append-only, so after a prompts/list refresh (list-changed notification or subscription) prompts removed by the server stayed exposed, an empty snapshot did not clear prompts, and changed descriptions/arguments stayed stale.

Description & Review Guide

  • What are the major changes?
    • A successful, complete prompts/list fetch (all pages) is now treated as the current prompt catalog and swapped into _functions in one step, mirroring how _load_tools_locked refreshes tools.
    • MCP tools and user-added functions are preserved; duplicate detection is seeded only with those non-prompt functions.
    • Unchanged prompt functions (same remote name, description and parameters) are reused so local customizations survive a refresh; changed prompts are rebuilt and reported through the function load callback.
    • Removed prompts are dropped from the progressively loaded names.
    • If any page request fails, nothing is published and the previous catalog is kept.
  • What is the impact of these changes? Prompt functions now track the server's current prompt catalog. No public API changes.
  • What do you want reviewers to focus on? Identification of prompt functions via a new _is_mcp_prompt_function helper (has an MCP remote name and is not marked as a tool), and the reuse rule for unchanged prompts.

Tests added: removed/changed/kept prompts with pagination while preserving tools and custom functions, empty snapshot clearing prompts, and failed pagination keeping the previous catalog.

Related Issue

Fixes #9115

Contribution Checklist

  • The code builds clean without any errors or warnings
  • All unit tests pass, and I have added new tests where possible
  • The PR follows the Contribution Guidelines
  • This PR is linked to an issue and there is no other open PR for this issue (see Related Issue above).
  • This is not a breaking change. If it is a breaking change, add the breaking change label (or add "[BREAKING]" to the title prefix, before or after any language prefix) — a workflow keeps the label and title prefix in sync automatically.

🤖 Generated with Claude Code

A complete prompts/list snapshot now replaces previously loaded prompt
functions instead of only appending unseen names, so removed prompts are
dropped, an empty snapshot clears prompts, and changed descriptions or
arguments are refreshed. MCP tools and user-added functions are preserved,
unchanged prompt functions are reused, and a failed page request keeps the
previous catalog.

Fixes microsoft#9115

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>

Copilot AI left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Copilot review overview

🟡 Changes recommended

Progressive disclosure can retain load state for a different remote prompt sharing the same normalized name.

Review effort: Balanced
Findings: 1 Medium severity

Open (1)
What changed in this PR

Refreshes MCP prompts as a complete catalog while preserving unrelated functions and unchanged prompts.

Changes:

  • Atomically replaces prompts after successful pagination.
  • Adds coverage for replacement, clearing, and pagination failures.
File Description
python/​packages/​core/​agent_framework/​_mcp.py Implements prompt catalog refresh and reuse.
python/​packages/​core/​tests/​core/​test_mcp.py Tests prompt refresh scenarios.

💡 Add a code-review agent skill for context-aware, tailored reviews. Learn more in the docs.

Comment thread python/packages/core/agent_framework/_mcp.py Outdated
@Anshuman-Prakash

Copy link
Copy Markdown
Contributor Author

@microsoft-github-policy-service agree

@eavanvalkenburg

Copy link
Copy Markdown
Member

Anshuman Prakash (@Anshuman-Prakash) The current refresh diff still computes removed prompts by comparing existing_prompts.keys() with {func.name for func in prompt_functions}, which are normalized exposed names rather than stable remote prompt identities. A catalog replacement such as remote prompt a/b → a-b can collide after normalization and retain the old prompt’s progressive-load state. This needs a stable remote-identity comparison and a collision regression before the refresh semantics are safe.

Track prompts reused with the same remote name during refresh and clear
progressive-load state for every other previously loaded prompt, so a
replacement that normalizes to the same local name (a/b -> a-b) starts
unloaded. Mirrors the tool refresh logic.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
@Anshuman-Prakash

Copy link
Copy Markdown
Contributor Author

Eduard van Valkenburg (@eavanvalkenburg) Thanks, good catch. Fixed in 4bb3ad9: the removed-prompt cleanup now compares stable remote identity instead of normalized names. Only prompts reused with a matching remote name keep their progressive-load state, the same way the tool refresh works. I added a collision regression test for a/b → a-b; the full test_mcp.py suite passes locally (398 passed).

Comment thread python/packages/core/agent_framework/_mcp.py Outdated
Track prompts that keep the same remote identity separately from prompts
whose FunctionTool object is reused, so a prompt rebuilt for a changed
description or arguments keeps its progressive-load state after refresh.
Adds a changed-metadata regression test.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Merged via the queue into microsoft:main with commit 4574872 Oct 13, 2026
45 checks passed

This branch was previously deployed

1 inactive deployment
github-app-auth — 6b8aeff9 Deployed Oct 8, 2026 by Anshuman-Prakash via add_label #24774
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

python Usage: [Issues, PRs], Target: Python

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Python: [Python] MCP prompt refresh keeps removed or changed prompts

3 participants