@@ -686,3 +686,201 @@ async def run_client() -> None:
686686 )
687687
688688 tg .cancel_scope .cancel ()
689+
690+
691+ @pytest .mark .anyio
692+ async def test_client_returns_error_for_unhandled_task_result_request (client_streams : ClientTestStreams ) -> None :
693+ """Test that client returns error for unhandled tasks/result request."""
694+ with anyio .fail_after (10 ):
695+ client_ready = anyio .Event ()
696+
697+ async with anyio .create_task_group () as tg :
698+
699+ async def run_client () -> None :
700+ async with ClientSession (
701+ client_streams .client_receive ,
702+ client_streams .client_send ,
703+ message_handler = _default_message_handler ,
704+ ):
705+ client_ready .set ()
706+ await anyio .sleep_forever ()
707+
708+ tg .start_soon (run_client )
709+ await client_ready .wait ()
710+
711+ typed_request = GetTaskPayloadRequest (params = GetTaskPayloadRequestParams (taskId = "nonexistent" ))
712+ request = types .JSONRPCRequest (
713+ jsonrpc = "2.0" ,
714+ id = "req-result" ,
715+ ** typed_request .model_dump (by_alias = True ),
716+ )
717+ await client_streams .server_send .send (SessionMessage (types .JSONRPCMessage (request )))
718+
719+ response_msg = await client_streams .server_receive .receive ()
720+ response = response_msg .message .root
721+ assert isinstance (response , types .JSONRPCError )
722+ assert "not supported" in response .error .message .lower ()
723+
724+ tg .cancel_scope .cancel ()
725+
726+
727+ @pytest .mark .anyio
728+ async def test_client_returns_error_for_unhandled_list_tasks_request (client_streams : ClientTestStreams ) -> None :
729+ """Test that client returns error for unhandled tasks/list request."""
730+ with anyio .fail_after (10 ):
731+ client_ready = anyio .Event ()
732+
733+ async with anyio .create_task_group () as tg :
734+
735+ async def run_client () -> None :
736+ async with ClientSession (
737+ client_streams .client_receive ,
738+ client_streams .client_send ,
739+ message_handler = _default_message_handler ,
740+ ):
741+ client_ready .set ()
742+ await anyio .sleep_forever ()
743+
744+ tg .start_soon (run_client )
745+ await client_ready .wait ()
746+
747+ typed_request = ListTasksRequest ()
748+ request = types .JSONRPCRequest (
749+ jsonrpc = "2.0" ,
750+ id = "req-list" ,
751+ ** typed_request .model_dump (by_alias = True ),
752+ )
753+ await client_streams .server_send .send (SessionMessage (types .JSONRPCMessage (request )))
754+
755+ response_msg = await client_streams .server_receive .receive ()
756+ response = response_msg .message .root
757+ assert isinstance (response , types .JSONRPCError )
758+ assert "not supported" in response .error .message .lower ()
759+
760+ tg .cancel_scope .cancel ()
761+
762+
763+ @pytest .mark .anyio
764+ async def test_client_returns_error_for_unhandled_cancel_task_request (client_streams : ClientTestStreams ) -> None :
765+ """Test that client returns error for unhandled tasks/cancel request."""
766+ with anyio .fail_after (10 ):
767+ client_ready = anyio .Event ()
768+
769+ async with anyio .create_task_group () as tg :
770+
771+ async def run_client () -> None :
772+ async with ClientSession (
773+ client_streams .client_receive ,
774+ client_streams .client_send ,
775+ message_handler = _default_message_handler ,
776+ ):
777+ client_ready .set ()
778+ await anyio .sleep_forever ()
779+
780+ tg .start_soon (run_client )
781+ await client_ready .wait ()
782+
783+ typed_request = CancelTaskRequest (params = CancelTaskRequestParams (taskId = "nonexistent" ))
784+ request = types .JSONRPCRequest (
785+ jsonrpc = "2.0" ,
786+ id = "req-cancel" ,
787+ ** typed_request .model_dump (by_alias = True ),
788+ )
789+ await client_streams .server_send .send (SessionMessage (types .JSONRPCMessage (request )))
790+
791+ response_msg = await client_streams .server_receive .receive ()
792+ response = response_msg .message .root
793+ assert isinstance (response , types .JSONRPCError )
794+ assert "not supported" in response .error .message .lower ()
795+
796+ tg .cancel_scope .cancel ()
797+
798+
799+ @pytest .mark .anyio
800+ async def test_client_returns_error_for_unhandled_task_augmented_sampling (client_streams : ClientTestStreams ) -> None :
801+ """Test that client returns error for task-augmented sampling without handler."""
802+ with anyio .fail_after (10 ):
803+ client_ready = anyio .Event ()
804+
805+ async with anyio .create_task_group () as tg :
806+
807+ async def run_client () -> None :
808+ # No task handlers provided - uses defaults
809+ async with ClientSession (
810+ client_streams .client_receive ,
811+ client_streams .client_send ,
812+ message_handler = _default_message_handler ,
813+ ):
814+ client_ready .set ()
815+ await anyio .sleep_forever ()
816+
817+ tg .start_soon (run_client )
818+ await client_ready .wait ()
819+
820+ # Send task-augmented sampling request
821+ typed_request = CreateMessageRequest (
822+ params = CreateMessageRequestParams (
823+ messages = [SamplingMessage (role = "user" , content = TextContent (type = "text" , text = "Hello" ))],
824+ maxTokens = 100 ,
825+ task = TaskMetadata (ttl = 60000 ),
826+ )
827+ )
828+ request = types .JSONRPCRequest (
829+ jsonrpc = "2.0" ,
830+ id = "req-sampling" ,
831+ ** typed_request .model_dump (by_alias = True ),
832+ )
833+ await client_streams .server_send .send (SessionMessage (types .JSONRPCMessage (request )))
834+
835+ response_msg = await client_streams .server_receive .receive ()
836+ response = response_msg .message .root
837+ assert isinstance (response , types .JSONRPCError )
838+ assert "not supported" in response .error .message .lower ()
839+
840+ tg .cancel_scope .cancel ()
841+
842+
843+ @pytest .mark .anyio
844+ async def test_client_returns_error_for_unhandled_task_augmented_elicitation (
845+ client_streams : ClientTestStreams ,
846+ ) -> None :
847+ """Test that client returns error for task-augmented elicitation without handler."""
848+ with anyio .fail_after (10 ):
849+ client_ready = anyio .Event ()
850+
851+ async with anyio .create_task_group () as tg :
852+
853+ async def run_client () -> None :
854+ # No task handlers provided - uses defaults
855+ async with ClientSession (
856+ client_streams .client_receive ,
857+ client_streams .client_send ,
858+ message_handler = _default_message_handler ,
859+ ):
860+ client_ready .set ()
861+ await anyio .sleep_forever ()
862+
863+ tg .start_soon (run_client )
864+ await client_ready .wait ()
865+
866+ # Send task-augmented elicitation request
867+ typed_request = ElicitRequest (
868+ params = ElicitRequestFormParams (
869+ message = "What is your name?" ,
870+ requestedSchema = {"type" : "object" , "properties" : {"name" : {"type" : "string" }}},
871+ task = TaskMetadata (ttl = 60000 ),
872+ )
873+ )
874+ request = types .JSONRPCRequest (
875+ jsonrpc = "2.0" ,
876+ id = "req-elicit" ,
877+ ** typed_request .model_dump (by_alias = True ),
878+ )
879+ await client_streams .server_send .send (SessionMessage (types .JSONRPCMessage (request )))
880+
881+ response_msg = await client_streams .server_receive .receive ()
882+ response = response_msg .message .root
883+ assert isinstance (response , types .JSONRPCError )
884+ assert "not supported" in response .error .message .lower ()
885+
886+ tg .cancel_scope .cancel ()
0 commit comments