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+ 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+
130137 async def handle_extension_request (self , method : str , params : Any ) -> Any :
131138 return {"method" : method , "params" : params }
132139
140+ async def handle_extension_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 :
@@ -146,6 +159,9 @@ async def initialize(
146159 async def handle_extension_request (self , method : str , params : Any ) -> Any :
147160 return {"method" : method , "params" : params }
148161
162+ async def handle_extension_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,10 +433,13 @@ 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 )
@@ -436,6 +455,11 @@ async def test_extension_and_notification_names_are_explicit() -> None:
436455 with pytest .raises (ValueError , match = "must start with '_'" ):
437456 await client_connection .send_extension_request ("vendor/do" )
438457
458+ await client_connection .send_extension_notification ("_vendor/event" , {"value" : 1 })
459+ await agent_connection .send_extension_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 })
462+
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" )
441465
@@ -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 , "handle_extension_notification" , autospec = True ) as notification :
492+ await client_connection .send_extension_notification ("_vendor/event" )
493+ with pytest .raises (RequestError , check = lambda error : error .code == - 32601 ):
494+ await client_connection .send_extension_request ("_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 ()
0 commit comments