MCPcopy
hub / github.com/modelcontextprotocol/python-sdk / mock_server

Function mock_server

tests/client/test_session.py:81–120  ·  view source on GitHub ↗
()

Source from the content-addressed store, hash-verified

79 result = None
80
81 async def mock_server():
82 nonlocal initialized_notification
83
84 session_message = await client_to_server_receive.receive()
85 jsonrpc_request = session_message.message
86 assert isinstance(jsonrpc_request, JSONRPCRequest)
87 request = client_request_adapter.validate_python(
88 jsonrpc_request.model_dump(by_alias=True, mode="json", exclude_none=True)
89 )
90 assert isinstance(request, InitializeRequest)
91
92 result = InitializeResult(
93 protocol_version=LATEST_HANDSHAKE_VERSION,
94 capabilities=ServerCapabilities(
95 logging=None,
96 resources=None,
97 tools=None,
98 experimental=None,
99 prompts=None,
100 ),
101 server_info=Implementation(name="mock-server", version="0.1.0"),
102 instructions="The server instructions.",
103 )
104
105 async with server_to_client_send:
106 await server_to_client_send.send(
107 SessionMessage(
108 JSONRPCResponse(
109 jsonrpc="2.0",
110 id=jsonrpc_request.id,
111 result=result.model_dump(by_alias=True, mode="json", exclude_none=True),
112 )
113 )
114 )
115 session_notification = await client_to_server_receive.receive()
116 jsonrpc_notification = session_notification.message
117 assert isinstance(jsonrpc_notification, JSONRPCNotification)
118 initialized_notification = client_notification_adapter.validate_python(
119 jsonrpc_notification.model_dump(by_alias=True, mode="json", exclude_none=True)
120 )
121
122 # Create a message handler to catch exceptions
123 async def message_handler( # pragma: no cover

Callers

nothing calls this directly

Calls 10

InitializeResultClass · 0.90
ServerCapabilitiesClass · 0.90
ImplementationClass · 0.90
SessionMessageClass · 0.90
JSONRPCResponseClass · 0.90
CallToolResultClass · 0.90
TextContentClass · 0.90
receiveMethod · 0.45
sendMethod · 0.45
closeMethod · 0.45

Tested by

no test coverage detected