Skip to content

fix(mcp): store empty dict when MCP prompt load fails - #693

Merged
sicoyle merged 2 commits into
dapr:mainfrom
anxkhn:fix/mcp-server-prompts-empty-dict
Jul 30, 2026
Merged

fix(mcp): store empty dict when MCP prompt load fails#693
sicoyle merged 2 commits into
dapr:mainfrom
anxkhn:fix/mcp-server-prompts-empty-dict

Conversation

@anxkhn

@anxkhn anxkhn commented Jul 10, 2026

Copy link
Copy Markdown
Contributor

Description

MCPClient._server_prompts is a dictionary of per-server prompt dictionaries, but the prompt-loading failure path stored a list. Prompt accessors then called .values(), .keys(), or .get() on that list and raised AttributeError. A server without prompt support could also break aggregate prompt lookups across all servers.

This changes the failure value from [] to {}, matching the declared type and success path. Regression tests cover prompt-loading failure and success paths.

Issue reference

No issue. This is a small, self-contained bug fix discovered while reviewing the MCP client.

Checklist

  • Created/updated tests
  • Tested this change against all the quickstarts
  • Extended the documentation

No documentation PR is needed because this fixes an internal container type without changing the public API, configuration, or documented behavior.

AGENTS.md Notes

  • The documented pre-commit checks ran successfully.
  • Adding the DCO sign-off requirement beside the commit-format section would help contributors avoid unsigned commits.

MCPClient._server_prompts is typed Dict[str, Dict[str, Prompt]] and every
accessor (get_server_prompts, get_all_prompts, get_prompt_names,
get_all_prompt_names, get_prompt_metadata) calls dict methods (.values(),
.keys(), .get()) on the stored value.

_load_prompts_from_session runs on every server connect right after
list_prompts(). MCP servers that do not implement the prompts capability
make list_prompts() raise, and the except branch stored a list ([]) instead
of a dict, so any subsequent prompt lookup raised
"AttributeError: 'list' object has no attribute 'values'". Because
get_all_prompts()/get_all_prompt_names() iterate every server, a single
prompt-less server poisoned the aggregate call for all servers.

Store {} to match the declared type and the success path. Add regression
tests covering the failure and success paths.

Signed-off-by: Anas Khan <83116240+anxkhn@users.noreply.github.com>
@anxkhn
anxkhn requested review from a team as code owners July 10, 2026 19:36
@anxkhn

anxkhn commented Jul 10, 2026

Copy link
Copy Markdown
Contributor Author

The E2E Tests (Ollama) check failed at the "Install Ollama" step with ollama: command not found, which means the curl -fsSL https://ollama.com/install.sh | sh install step did not succeed on that runner. This is unrelated to this change: the diff only touches dapr_agents/tool/mcp/client.py (a one-character container-type fix) plus a unit test under tests/tool/, and it adds no integration tests that run in the Ollama e2e path. Lint, all build matrix jobs (3.11-3.14), schema checks, and DCO are green. Could a maintainer re-run the failed job? Happy to rebase if that helps re-trigger it.

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.

Pull request overview

Fixes a container-type bug in MCPClient prompt loading so prompt accessors remain safe when an MCP server does not support the prompts capability (or prompt loading otherwise fails). This aligns the failure-path storage with the declared _server_prompts: Dict[str, Dict[str, Prompt]] type and prevents AttributeError in aggregate prompt lookups.

Changes:

  • Store {} (empty dict) instead of [] (list) in _server_prompts[server_name] when list_prompts() fails.
  • Add regression tests covering both prompt-loading failure and success paths for the prompt accessors.

Reviewed changes

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

File Description
dapr_agents/tool/mcp/client.py Fixes prompt-load failure path to store an empty mapping (dict) so .values()/.keys()/.get() accessors remain valid.
tests/tool/test_mcp_client.py Adds async regression tests to ensure prompt accessors behave correctly on both failure and success prompt-loading paths.

💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.

@sicoyle sicoyle left a comment

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

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

amazing, thank you!!! 🚀

@sicoyle
sicoyle merged commit d44c15c into dapr:main Jul 30, 2026
8 of 9 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants