Skip to content
Closed
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 10 additions & 0 deletions src/mcp/server/mcpserver/server.py
Original file line number Diff line number Diff line change
Expand Up @@ -1283,6 +1283,7 @@ async def get_prompt(
"""
if context is None:
context = Context(mcp_server=self, subscriptions=self._subscriptions)

try:
prompt = self._prompt_manager.get_prompt(name)
if not prompt:
Expand All @@ -1296,8 +1297,17 @@ async def get_prompt(
description=prompt.description,
messages=pydantic_core.to_jsonable_python(rendered),
)

except MCPError:
raise

except ValueError as e:
Comment thread
cubic-dev-ai[bot] marked this conversation as resolved.
Resolved
# Expected user-input validation failures, like missing required
# arguments, don't need a full traceback for unexpected errors.
# Instead, log a concise warning without exc_info.
logger.warning(f"Error getting prompt {name}: {e}")
raise ValueError(str(e)) from e

except Exception as e:
logger.exception(f"Error getting prompt {name}")
raise ValueError(str(e)) from e
Expand Down
30 changes: 30 additions & 0 deletions tests/server/mcpserver/test_server.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import base64
import logging
from pathlib import Path
from types import SimpleNamespace
from typing import Any
Expand Down Expand Up @@ -1527,6 +1528,35 @@ def prompt_fn(name: str) -> str: ... # pragma: no branch
with pytest.raises(MCPError, match="Missing required arguments"):
await client.get_prompt("prompt_fn")

async def test_get_prompt_missing_args_logs_warning_without_traceback(
self, caplog: pytest.LogCaptureFixture
) -> None:
"""Regression for issue #3342: missing-argument ValueErrors are an expected
validation failure, so `MCPServer.get_prompt`'s own logger should emit
a plain warning without exc_info, instead of a full traceback.

Note: `jsonrpc_dispatcher` has a separate, intentional catch-all that
logs a traceback for any handler exception it doesn't recognize as
`MCPError`/`ValidationError`. However, that generic safety net is out of
scope here and unaffected by this fix PR #3347.
"""
mcp = MCPServer()

@mcp.prompt()
def prompt_fn(name: str) -> str: ... # Pragma: no branch.

with caplog.at_level(logging.WARNING, logger="mcp.server.mcpserver.server"):
async with Client(mcp, mode="legacy") as client:
with pytest.raises(MCPError, match="Missing required arguments"):
await client.get_prompt("prompt_fn")

server_records = [r for r in caplog.records if r.name == "mcp.server.mcpserver.server"]
assert len(server_records) == 1

# ValueError should have warning log without exc_info.
assert server_records[0].levelno == logging.WARNING
assert not server_records[0].exc_info


async def test_resource_decorator_rfc6570_reserved_expansion():
# Regression: old regex-based param extraction couldn't see `path`
Expand Down
Loading