Skip to content

Commit d38b378

Browse files
test(fastmcp): Narrow AttributeError try-except (#5339)
1 parent e73e600 commit d38b378

File tree

1 file changed

+64
-64
lines changed

1 file changed

+64
-64
lines changed

tests/integrations/fastmcp/test_fastmcp.py

Lines changed: 64 additions & 64 deletions
Original file line numberDiff line numberDiff line change
@@ -569,49 +569,49 @@ def test_fastmcp_prompt_sync(
569569
request_ctx.set(mock_ctx)
570570

571571
# Try to register a prompt handler (may not be supported in all versions)
572-
try:
573-
if hasattr(mcp, "prompt"):
574-
575-
@mcp.prompt()
576-
def code_help_prompt(language: str):
577-
"""Get help for a programming language"""
578-
return [
579-
{
580-
"role": "user",
581-
"content": {
582-
"type": "text",
583-
"text": f"Tell me about {language}",
584-
},
585-
}
586-
]
572+
if hasattr(mcp, "prompt"):
573+
574+
@mcp.prompt()
575+
def code_help_prompt(language: str):
576+
"""Get help for a programming language"""
577+
return [
578+
{
579+
"role": "user",
580+
"content": {
581+
"type": "text",
582+
"text": f"Tell me about {language}",
583+
},
584+
}
585+
]
587586

588-
with start_transaction(name="fastmcp tx"):
587+
with start_transaction(name="fastmcp tx"):
588+
try:
589589
result = call_prompt_through_mcp(
590590
mcp, "code_help_prompt", {"language": "python"}
591591
)
592-
593-
assert result.messages[0].role == "user"
594-
assert "python" in result.messages[0].content.text.lower()
595-
596-
(tx,) = events
597-
assert tx["type"] == "transaction"
598-
599-
# Verify prompt span was created
600-
prompt_spans = [s for s in tx["spans"] if s["op"] == OP.MCP_SERVER]
601-
assert len(prompt_spans) == 1
602-
span = prompt_spans[0]
603-
assert span["origin"] == "auto.ai.mcp"
604-
assert span["description"] == "prompts/get code_help_prompt"
605-
assert span["data"][SPANDATA.MCP_PROMPT_NAME] == "code_help_prompt"
606-
607-
# Check PII-sensitive data
608-
if send_default_pii and include_prompts:
609-
assert SPANDATA.MCP_PROMPT_RESULT_MESSAGE_CONTENT in span["data"]
610-
else:
611-
assert SPANDATA.MCP_PROMPT_RESULT_MESSAGE_CONTENT not in span["data"]
612-
except AttributeError:
613-
# Prompt handler not supported in this version
614-
pytest.skip("Prompt handlers not supported in this FastMCP version")
592+
except AttributeError:
593+
# Prompt handler not supported in this version
594+
pytest.skip("Prompt handlers not supported in this FastMCP version")
595+
596+
assert result.messages[0].role == "user"
597+
assert "python" in result.messages[0].content.text.lower()
598+
599+
(tx,) = events
600+
assert tx["type"] == "transaction"
601+
602+
# Verify prompt span was created
603+
prompt_spans = [s for s in tx["spans"] if s["op"] == OP.MCP_SERVER]
604+
assert len(prompt_spans) == 1
605+
span = prompt_spans[0]
606+
assert span["origin"] == "auto.ai.mcp"
607+
assert span["description"] == "prompts/get code_help_prompt"
608+
assert span["data"][SPANDATA.MCP_PROMPT_NAME] == "code_help_prompt"
609+
610+
# Check PII-sensitive data
611+
if send_default_pii and include_prompts:
612+
assert SPANDATA.MCP_PROMPT_RESULT_MESSAGE_CONTENT in span["data"]
613+
else:
614+
assert SPANDATA.MCP_PROMPT_RESULT_MESSAGE_CONTENT not in span["data"]
615615

616616

617617
@pytest.mark.parametrize("FastMCP", fastmcp_implementations, ids=fastmcp_ids)
@@ -634,38 +634,38 @@ async def test_fastmcp_prompt_async(sentry_init, capture_events, FastMCP):
634634
request_ctx.set(mock_ctx)
635635

636636
# Try to register an async prompt handler
637-
try:
638-
if hasattr(mcp, "prompt"):
639-
640-
@mcp.prompt()
641-
async def async_prompt(topic: str):
642-
"""Get async prompt for a topic"""
643-
return [
644-
{
645-
"role": "user",
646-
"content": {"type": "text", "text": f"What is {topic}?"},
647-
},
648-
{
649-
"role": "assistant",
650-
"content": {
651-
"type": "text",
652-
"text": "Let me explain that",
653-
},
637+
if hasattr(mcp, "prompt"):
638+
639+
@mcp.prompt()
640+
async def async_prompt(topic: str):
641+
"""Get async prompt for a topic"""
642+
return [
643+
{
644+
"role": "user",
645+
"content": {"type": "text", "text": f"What is {topic}?"},
646+
},
647+
{
648+
"role": "assistant",
649+
"content": {
650+
"type": "text",
651+
"text": "Let me explain that",
654652
},
655-
]
653+
},
654+
]
656655

657-
with start_transaction(name="fastmcp tx"):
656+
with start_transaction(name="fastmcp tx"):
657+
try:
658658
result = await call_prompt_through_mcp_async(
659659
mcp, "async_prompt", {"topic": "MCP"}
660660
)
661+
except AttributeError:
662+
# Prompt handler not supported in this version
663+
pytest.skip("Prompt handlers not supported in this FastMCP version")
661664

662-
assert len(result.messages) == 2
665+
assert len(result.messages) == 2
663666

664-
(tx,) = events
665-
assert tx["type"] == "transaction"
666-
except AttributeError:
667-
# Prompt handler not supported in this version
668-
pytest.skip("Prompt handlers not supported in this FastMCP version")
667+
(tx,) = events
668+
assert tx["type"] == "transaction"
669669

670670

671671
# =============================================================================

0 commit comments

Comments
 (0)