@@ -963,6 +963,79 @@ def code_help_prompt(language: str):
963963 assert SPANDATA .MCP_PROMPT_RESULT_MESSAGE_CONTENT not in span ["data" ]
964964
965965
966+ @pytest .mark .parametrize ("FastMCP" , fastmcp_implementations , ids = fastmcp_ids )
967+ @pytest .mark .asyncio
968+ async def test_fastmcp_prompt_async (
969+ sentry_init ,
970+ capture_events ,
971+ FastMCP ,
972+ json_rpc ,
973+ select_transactions_with_mcp_spans ,
974+ ):
975+ """Test that FastMCP async prompt handlers create proper spans"""
976+ sentry_init (
977+ integrations = [MCPIntegration ()],
978+ traces_sample_rate = 1.0 ,
979+ )
980+ events = capture_events ()
981+
982+ mcp = FastMCP ("Test Server" )
983+
984+ session_manager = StreamableHTTPSessionManager (
985+ app = mcp ._mcp_server ,
986+ json_response = True ,
987+ )
988+
989+ app = Starlette (
990+ routes = [
991+ Mount ("/mcp" , app = session_manager .handle_request ),
992+ ],
993+ lifespan = lambda app : session_manager .run (),
994+ )
995+
996+ # Try to register an async prompt handler
997+ if hasattr (mcp , "prompt" ):
998+
999+ @mcp .prompt ()
1000+ async def async_prompt (topic : str ):
1001+ """Get async prompt for a topic"""
1002+ message1 = {
1003+ "role" : "user" ,
1004+ "content" : {"type" : "text" , "text" : f"What is { topic } ?" },
1005+ }
1006+
1007+ message2 = {
1008+ "role" : "assistant" ,
1009+ "content" : {
1010+ "type" : "text" ,
1011+ "text" : "Let me explain that" ,
1012+ },
1013+ }
1014+
1015+ if FASTMCP_VERSION is not None and FASTMCP_VERSION .startswith ("3" ):
1016+ message1 = Message (message1 )
1017+ message2 = Message (message2 )
1018+
1019+ return [message1 , message2 ]
1020+
1021+ _ , result = json_rpc (
1022+ app ,
1023+ method = "prompts/get" ,
1024+ params = {
1025+ "name" : "async_prompt" ,
1026+ "arguments" : {"topic" : "MCP" },
1027+ },
1028+ request_id = "req-async-prompt" ,
1029+ )
1030+
1031+ assert len (result .json ()["result" ]["messages" ]) == 2
1032+
1033+ transactions = select_transactions_with_mcp_spans (
1034+ events , method_name = "prompts/get"
1035+ )
1036+ assert len (transactions ) == 1
1037+
1038+
9661039# =============================================================================
9671040# Resource Handler Tests (if supported)
9681041# =============================================================================
0 commit comments