Skip to content

Commit 70edb6d

Browse files
cleaning up
1 parent a8372b0 commit 70edb6d

File tree

1 file changed

+26
-54
lines changed

1 file changed

+26
-54
lines changed

tests/integrations/mcp/test_mcp.py

Lines changed: 26 additions & 54 deletions
Original file line numberDiff line numberDiff line change
@@ -81,10 +81,7 @@ def get_mcp_command_payload(method: str, params, request_id: str):
8181
)
8282

8383

84-
async def stdio(server, method: str, params, request_id: str | None = None):
85-
if request_id is None:
86-
request_id = "1" # arbitrary
87-
84+
async def stdio(server, method: str, params, request_id: str):
8885
read_stream_writer, read_stream = anyio.create_memory_object_stream(0)
8986
write_stream, write_stream_reader = anyio.create_memory_object_stream(0)
9087

@@ -355,7 +352,7 @@ async def test_tool_handler_with_error(sentry_init, capture_events):
355352
server = Server("test-server")
356353

357354
@server.call_tool()
358-
async def failing_tool(tool_name, arguments):
355+
def failing_tool(tool_name, arguments):
359356
raise ValueError("Tool execution failed")
360357

361358
with start_transaction(name="mcp tx"):
@@ -366,6 +363,7 @@ async def failing_tool(tool_name, arguments):
366363
"name": "bad_tool",
367364
"arguments": {},
368365
},
366+
request_id="req-error",
369367
)
370368

371369
assert (
@@ -547,7 +545,7 @@ async def failing_prompt(name, arguments):
547545
"name": "code_help",
548546
"arguments": {"language": "python"},
549547
},
550-
request_id="req-prompt",
548+
request_id="req-error-prompt",
551549
)
552550

553551
assert response.message.root.error.message == "Prompt not found"
@@ -664,7 +662,7 @@ async def test_resource_handler_with_error(sentry_init, capture_events):
664662
server = Server("test-server")
665663

666664
@server.read_resource()
667-
async def failing_resource(uri):
665+
def failing_resource(uri):
668666
raise FileNotFoundError("Resource not found")
669667

670668
with start_transaction(name="mcp tx"):
@@ -674,6 +672,7 @@ async def failing_resource(uri):
674672
params={
675673
"uri": "file:///missing.txt",
676674
},
675+
request_id="req-error-resource",
677676
)
678677

679678
# Should have error event and transaction
@@ -703,7 +702,7 @@ async def test_tool_result_extraction_tuple(
703702
server = Server("test-server")
704703

705704
@server.call_tool()
706-
async def test_tool_tuple(tool_name, arguments):
705+
def test_tool_tuple(tool_name, arguments):
707706
# Return CombinationContent: (UnstructuredContent, StructuredContent)
708707
unstructured = [MockTextContent("Result text")]
709708
structured = {"key": "value", "count": 5}
@@ -715,8 +714,9 @@ async def test_tool_tuple(tool_name, arguments):
715714
method="tools/call",
716715
params={
717716
"name": "calculate",
718-
"arguments": {"x": 10, "y": 5},
717+
"arguments": {},
719718
},
719+
request_id="req-tuple",
720720
)
721721

722722
(tx,) = events
@@ -755,7 +755,7 @@ async def test_tool_result_extraction_unstructured(
755755
server = Server("test-server")
756756

757757
@server.call_tool()
758-
async def test_tool_unstructured(tool_name, arguments):
758+
def test_tool_unstructured(tool_name, arguments):
759759
# Return UnstructuredContent as list of content blocks
760760
return [
761761
MockTextContent("First part"),
@@ -767,9 +767,10 @@ async def test_tool_unstructured(tool_name, arguments):
767767
server,
768768
method="tools/call",
769769
params={
770-
"name": "calculate",
771-
"arguments": {"x": 10, "y": 5},
770+
"name": "text_tool",
771+
"arguments": {},
772772
},
773+
request_id="req-unstructured",
773774
)
774775

775776
(tx,) = events
@@ -784,43 +785,6 @@ async def test_tool_unstructured(tool_name, arguments):
784785
assert SPANDATA.MCP_TOOL_RESULT_CONTENT not in span["data"]
785786

786787

787-
def test_request_context_no_context(sentry_init, capture_events):
788-
"""Test handling when no request context is available"""
789-
sentry_init(
790-
integrations=[MCPIntegration()],
791-
traces_sample_rate=1.0,
792-
)
793-
events = capture_events()
794-
795-
server = Server("test-server")
796-
797-
# Clear request context (simulating no context available)
798-
# This will cause a LookupError when trying to get context
799-
request_ctx.set(None)
800-
801-
@server.call_tool()
802-
def test_tool_no_ctx(tool_name, arguments):
803-
return {"result": "ok"}
804-
805-
with start_transaction(name="mcp tx"):
806-
# This should work even without request context
807-
try:
808-
test_tool_no_ctx("tool", {})
809-
except LookupError:
810-
# If it raises LookupError, that's expected when context is truly missing
811-
pass
812-
813-
# Should still create span even if context is missing
814-
(tx,) = events
815-
span = tx["spans"][0]
816-
817-
# Transport defaults to "pipe" when no context
818-
assert span["data"][SPANDATA.MCP_TRANSPORT] == "stdio"
819-
# Request ID and Session ID should not be present
820-
assert SPANDATA.MCP_REQUEST_ID not in span["data"]
821-
assert SPANDATA.MCP_SESSION_ID not in span["data"]
822-
823-
824788
@pytest.mark.asyncio
825789
async def test_span_origin(sentry_init, capture_events):
826790
"""Test that span origin is set correctly"""
@@ -833,7 +797,7 @@ async def test_span_origin(sentry_init, capture_events):
833797
server = Server("test-server")
834798

835799
@server.call_tool()
836-
async def test_tool(tool_name, arguments):
800+
def test_tool(tool_name, arguments):
837801
return {"result": "test"}
838802

839803
with start_transaction(name="mcp tx"):
@@ -844,6 +808,7 @@ async def test_tool(tool_name, arguments):
844808
"name": "calculate",
845809
"arguments": {"x": 10, "y": 5},
846810
},
811+
request_id="req-origin",
847812
)
848813

849814
(tx,) = events
@@ -864,15 +829,15 @@ async def test_multiple_handlers(sentry_init, capture_events):
864829
server = Server("test-server")
865830

866831
@server.call_tool()
867-
async def tool1(tool_name, arguments):
832+
def tool1(tool_name, arguments):
868833
return {"result": "tool1"}
869834

870835
@server.call_tool()
871-
async def tool2(tool_name, arguments):
836+
def tool2(tool_name, arguments):
872837
return {"result": "tool2"}
873838

874839
@server.get_prompt()
875-
async def prompt1(name, arguments):
840+
def prompt1(name, arguments):
876841
return GetPromptResult(
877842
description="A test prompt",
878843
messages=[
@@ -890,6 +855,7 @@ async def prompt1(name, arguments):
890855
"name": "tool_a",
891856
"arguments": {},
892857
},
858+
request_id="req-multi",
893859
)
894860

895861
await stdio(
@@ -899,6 +865,7 @@ async def prompt1(name, arguments):
899865
"name": "tool_b",
900866
"arguments": {},
901867
},
868+
request_id="req-multi",
902869
)
903870

904871
await stdio(
@@ -908,6 +875,7 @@ async def prompt1(name, arguments):
908875
"name": "prompt_a",
909876
"arguments": {},
910877
},
878+
request_id="req-multi",
911879
)
912880

913881
(tx,) = events
@@ -959,6 +927,7 @@ def test_prompt_dict(name, arguments):
959927
"name": "dict_prompt",
960928
"arguments": {},
961929
},
930+
request_id="req-dict-prompt",
962931
)
963932

964933
(tx,) = events
@@ -1002,6 +971,7 @@ def test_resource(uri):
1002971
params={
1003972
"uri": "https://example.com/resource",
1004973
},
974+
request_id="req-no-proto",
1005975
)
1006976

1007977
(tx,) = events
@@ -1024,7 +994,7 @@ async def test_tool_with_complex_arguments(sentry_init, capture_events):
1024994
server = Server("test-server")
1025995

1026996
@server.call_tool()
1027-
async def test_tool_complex(tool_name, arguments):
997+
def test_tool_complex(tool_name, arguments):
1028998
return {"processed": True}
1029999

10301000
with start_transaction(name="mcp tx"):
@@ -1040,6 +1010,7 @@ async def test_tool_complex(tool_name, arguments):
10401010
"name": "complex_tool",
10411011
"arguments": complex_args,
10421012
},
1013+
request_id="req-complex",
10431014
)
10441015

10451016
(tx,) = events
@@ -1181,6 +1152,7 @@ async def test_tool(tool_name, arguments):
11811152
"name": "stdio_tool",
11821153
"arguments": {},
11831154
},
1155+
request_id="req-stdio",
11841156
)
11851157

11861158
assert result.message.root.result["structuredContent"] == {"result": "success"}

0 commit comments

Comments
 (0)