44import json
55import socket
66from typing import Any
7+ from unittest .mock import patch
78
89import pytest
910from pydantic import ValidationError
@@ -126,15 +127,27 @@ async def echo_prompt(self, request: v2.schema.PromptRequest) -> None:
126127 )
127128
128129
129- class ExtensionClient :
130- async def handle_extension_request (self , method : str , params : Any ) -> Any :
130+ class ExtensionClient (v2 .Client ):
131+ def __init__ (self ) -> None :
132+ self .notifications : asyncio .Queue [tuple [str , Any ]] = asyncio .Queue ()
133+
134+ def on_connect (self , conn : v2 .ClientSideConnection ) -> None :
135+ self .connection = conn
136+
137+ async def ext_method (self , method : str , params : Any ) -> Any :
131138 return {"method" : method , "params" : params }
132139
140+ async def ext_notification (self , method : str , params : Any ) -> None :
141+ await self .notifications .put ((method , params ))
142+
133143
134- class ExtensionAgent :
144+ class ExtensionAgent ( v2 . Agent ) :
135145 def __init__ (self ) -> None :
136146 self .notifications : asyncio .Queue [tuple [str , Any ]] = asyncio .Queue ()
137147
148+ def on_connect (self , conn : v2 .AgentSideConnection ) -> None :
149+ self .connection = conn
150+
138151 async def initialize (
139152 self , protocol_version : int , info : v2 .schema .Implementation , ** kwargs : Any
140153 ) -> v2 .schema .InitializeResponse :
@@ -143,9 +156,12 @@ async def initialize(
143156 info = v2 .schema .Implementation (name = "extension-agent" , version = "1.0.0" ),
144157 )
145158
146- async def handle_extension_request (self , method : str , params : Any ) -> Any :
159+ async def ext_method (self , method : str , params : Any ) -> Any :
147160 return {"method" : method , "params" : params }
148161
162+ async def ext_notification (self , method : str , params : Any ) -> None :
163+ await self .notifications .put ((method , params ))
164+
149165 async def cancel_session (self , session_id : str , ** kwargs : Any ) -> None :
150166 notification = v2 .schema .CancelSessionNotification (session_id = session_id , ** kwargs )
151167 await self .notifications .put (("cancel" , notification ))
@@ -417,24 +433,32 @@ def test_v2_public_entry_point_is_explicit() -> None:
417433async def test_extension_and_notification_names_are_explicit () -> None :
418434 client_transport , agent_transport = memory_transport_pair ()
419435 agent = ExtensionAgent ()
436+ client = ExtensionClient ()
420437 agent_connection = v2 .AgentSideConnection (agent , agent_transport )
421- client_connection = v2 .ClientSideConnection (ExtensionClient () , client_transport )
438+ client_connection = v2 .ClientSideConnection (client , client_transport )
422439
423440 try :
441+ assert agent .connection is agent_connection
442+ assert client .connection is client_connection
424443 await client_connection .initialize (
425444 protocol_version = v2 .PROTOCOL_VERSION , info = v2 .schema .Implementation (name = "test-client" , version = "1.0.0" )
426445 )
427446
428- assert await client_connection .send_extension_request ("_vendor/do" , {"value" : 1 }) == {
447+ assert await client_connection .ext_method ("_vendor/do" , {"value" : 1 }) == {
429448 "method" : "_vendor/do" ,
430449 "params" : {"value" : 1 },
431450 }
432- assert await agent_connection .send_extension_request ("_vendor/read" , {"value" : 2 }) == {
451+ assert await agent_connection .ext_method ("_vendor/read" , {"value" : 2 }) == {
433452 "method" : "_vendor/read" ,
434453 "params" : {"value" : 2 },
435454 }
436455 with pytest .raises (ValueError , match = "must start with '_'" ):
437- await client_connection .send_extension_request ("vendor/do" )
456+ await client_connection .ext_method ("vendor/do" )
457+
458+ await client_connection .ext_notification ("_vendor/event" , {"value" : 1 })
459+ await agent_connection .ext_notification ("_vendor/event" , {"value" : 2 })
460+ assert await asyncio .wait_for (agent .notifications .get (), timeout = 1 ) == ("_vendor/event" , {"value" : 1 })
461+ assert await asyncio .wait_for (client .notifications .get (), timeout = 1 ) == ("_vendor/event" , {"value" : 2 })
438462
439463 await client_connection .cancel_session (session_id = "session-1" )
440464 await client_connection .notify_mcp (server_id = "mcp-1" , request_id = "r" , method = "notifications/progress" )
@@ -448,6 +472,29 @@ async def test_extension_and_notification_names_are_explicit() -> None:
448472 await agent_connection .close ()
449473
450474
475+ @pytest .mark .asyncio
476+ async def test_inherited_extension_hooks_are_unimplemented () -> None :
477+ class StubAgent (v2 .Agent ):
478+ async def initialize (
479+ self , protocol_version : int , info : v2 .schema .Implementation , ** kwargs : Any
480+ ) -> v2 .schema .InitializeResponse :
481+ return v2 .schema .InitializeResponse (protocol_version = protocol_version , info = info )
482+
483+ client_transport , agent_transport = memory_transport_pair ()
484+ async with (
485+ v2 .AgentSideConnection (StubAgent (), agent_transport ),
486+ v2 .ClientSideConnection (Client (), client_transport ) as client_connection ,
487+ ):
488+ request = initialize_request ()
489+ await client_connection .initialize (protocol_version = request .protocol_version , info = request .info )
490+
491+ with patch .object (v2 .Agent , "ext_notification" , autospec = True ) as notification :
492+ await client_connection .ext_notification ("_vendor/event" )
493+ with pytest .raises (RequestError , check = lambda error : error .code == - 32601 ):
494+ await client_connection .ext_method ("_vendor/ping" )
495+ notification .assert_not_called ()
496+
497+
451498@pytest .mark .asyncio
452499async def test_unhandled_notifications_are_ignored (caplog : pytest .LogCaptureFixture ) -> None :
453500 client_transport , agent_transport = memory_transport_pair ()
@@ -459,7 +506,7 @@ async def test_unhandled_notifications_are_ignored(caplog: pytest.LogCaptureFixt
459506 protocol_version = v2 .PROTOCOL_VERSION , info = v2 .schema .Implementation (name = "test-client" , version = "1.0.0" )
460507 )
461508 await agent_connection .session_update (session_id = "session-1" , update = v2 .schema .IdleSessionStateUpdate ())
462- await client_connection .send_extension_notification ("_vendor/event" )
509+ await client_connection .ext_notification ("_vendor/event" )
463510 await asyncio .sleep (0 )
464511 await asyncio .sleep (0 )
465512
@@ -556,7 +603,7 @@ async def test_cancelling_a_request_cancels_the_remote_handler() -> None:
556603 cancelled = asyncio .Event ()
557604
558605 class SlowAgent (ExtensionAgent ):
559- async def handle_extension_request (self , method : str , params : Any ) -> Any :
606+ async def ext_method (self , method : str , params : Any ) -> Any :
560607 started .set ()
561608 try :
562609 await asyncio .Event ().wait ()
@@ -572,7 +619,7 @@ async def handle_extension_request(self, method: str, params: Any) -> Any:
572619 await client_connection .initialize (
573620 protocol_version = v2 .PROTOCOL_VERSION , info = v2 .schema .Implementation (name = "test-client" , version = "1.0.0" )
574621 )
575- request = asyncio .create_task (client_connection .send_extension_request ("_vendor/slow" ))
622+ request = asyncio .create_task (client_connection .ext_method ("_vendor/slow" ))
576623 await asyncio .wait_for (started .wait (), timeout = 1 )
577624
578625 request .cancel ()
@@ -590,7 +637,7 @@ async def test_cancel_request_before_the_handler_starts_still_replies() -> None:
590637 handled : list [str ] = []
591638
592639 class RecordingAgent (ExtensionAgent ):
593- async def handle_extension_request (self , method : str , params : Any ) -> Any :
640+ async def ext_method (self , method : str , params : Any ) -> Any :
594641 handled .append (method )
595642 return {}
596643
0 commit comments