feat(client): store InitializeResult as initialize_result (#2300)
This commit is contained in:
parent
67201a9bbd
commit
20dd94632e
4 changed files with 64 additions and 37 deletions
|
|
@ -99,7 +99,7 @@ def app() -> MCPServer:
|
|||
async def test_client_is_initialized(app: MCPServer):
|
||||
"""Test that the client is initialized after entering context."""
|
||||
async with Client(app) as client:
|
||||
assert client.server_capabilities == snapshot(
|
||||
assert client.initialize_result.capabilities == snapshot(
|
||||
ServerCapabilities(
|
||||
experimental={},
|
||||
prompts=PromptsCapability(list_changed=False),
|
||||
|
|
@ -107,6 +107,7 @@ async def test_client_is_initialized(app: MCPServer):
|
|||
tools=ToolsCapability(list_changed=False),
|
||||
)
|
||||
)
|
||||
assert client.initialize_result.server_info.name == "test"
|
||||
|
||||
|
||||
async def test_client_with_simple_server(simple_server: Server):
|
||||
|
|
|
|||
|
|
@ -540,8 +540,8 @@ async def test_client_capabilities_with_sampling_tools():
|
|||
|
||||
|
||||
@pytest.mark.anyio
|
||||
async def test_get_server_capabilities():
|
||||
"""Test that get_server_capabilities returns None before init and capabilities after"""
|
||||
async def test_initialize_result():
|
||||
"""Test that initialize_result is None before init and contains the full result after."""
|
||||
client_to_server_send, client_to_server_receive = anyio.create_memory_object_stream[SessionMessage](1)
|
||||
server_to_client_send, server_to_client_receive = anyio.create_memory_object_stream[SessionMessage](1)
|
||||
|
||||
|
|
@ -551,6 +551,8 @@ async def test_get_server_capabilities():
|
|||
resources=types.ResourcesCapability(subscribe=True, list_changed=True),
|
||||
tools=types.ToolsCapability(list_changed=False),
|
||||
)
|
||||
expected_server_info = Implementation(name="mock-server", version="0.1.0")
|
||||
expected_instructions = "Use the tools wisely."
|
||||
|
||||
async def mock_server():
|
||||
session_message = await client_to_server_receive.receive()
|
||||
|
|
@ -564,7 +566,8 @@ async def test_get_server_capabilities():
|
|||
result = InitializeResult(
|
||||
protocol_version=LATEST_PROTOCOL_VERSION,
|
||||
capabilities=expected_capabilities,
|
||||
server_info=Implementation(name="mock-server", version="0.1.0"),
|
||||
server_info=expected_server_info,
|
||||
instructions=expected_instructions,
|
||||
)
|
||||
|
||||
async with server_to_client_send:
|
||||
|
|
@ -590,21 +593,17 @@ async def test_get_server_capabilities():
|
|||
server_to_client_send,
|
||||
server_to_client_receive,
|
||||
):
|
||||
assert session.get_server_capabilities() is None
|
||||
assert session.initialize_result is None
|
||||
|
||||
tg.start_soon(mock_server)
|
||||
await session.initialize()
|
||||
|
||||
capabilities = session.get_server_capabilities()
|
||||
assert capabilities is not None
|
||||
assert capabilities == expected_capabilities
|
||||
assert capabilities.logging is not None
|
||||
assert capabilities.prompts is not None
|
||||
assert capabilities.prompts.list_changed is True
|
||||
assert capabilities.resources is not None
|
||||
assert capabilities.resources.subscribe is True
|
||||
assert capabilities.tools is not None
|
||||
assert capabilities.tools.list_changed is False
|
||||
result = session.initialize_result
|
||||
assert result is not None
|
||||
assert result.server_info == expected_server_info
|
||||
assert result.capabilities == expected_capabilities
|
||||
assert result.instructions == expected_instructions
|
||||
assert result.protocol_version == LATEST_PROTOCOL_VERSION
|
||||
|
||||
|
||||
@pytest.mark.anyio
|
||||
|
|
|
|||
|
|
@ -69,7 +69,7 @@ async def test_with_mcpserver(mcpserver_server: MCPServer):
|
|||
async def test_server_is_running(mcpserver_server: MCPServer):
|
||||
"""Test that the server is running and responding to requests."""
|
||||
async with Client(mcpserver_server) as client:
|
||||
assert client.server_capabilities is not None
|
||||
assert client.initialize_result.capabilities.tools is not None
|
||||
|
||||
|
||||
async def test_list_tools(mcpserver_server: MCPServer):
|
||||
|
|
|
|||
|
|
@ -109,8 +109,7 @@ async def elicitation_callback(context: RequestContext[ClientSession], params: E
|
|||
async def test_basic_tools() -> None:
|
||||
"""Test basic tool functionality."""
|
||||
async with Client(basic_tool.mcp) as client:
|
||||
assert client.server_capabilities is not None
|
||||
assert client.server_capabilities.tools is not None
|
||||
assert client.initialize_result.capabilities.tools is not None
|
||||
|
||||
# Test sum tool
|
||||
tool_result = await client.call_tool("sum", {"a": 5, "b": 3})
|
||||
|
|
@ -128,8 +127,7 @@ async def test_basic_tools() -> None:
|
|||
async def test_basic_resources() -> None:
|
||||
"""Test basic resource functionality."""
|
||||
async with Client(basic_resource.mcp) as client:
|
||||
assert client.server_capabilities is not None
|
||||
assert client.server_capabilities.resources is not None
|
||||
assert client.initialize_result.capabilities.resources is not None
|
||||
|
||||
# Test document resource
|
||||
doc_content = await client.read_resource("file://documents/readme")
|
||||
|
|
@ -151,8 +149,7 @@ async def test_basic_resources() -> None:
|
|||
async def test_basic_prompts() -> None:
|
||||
"""Test basic prompt functionality."""
|
||||
async with Client(basic_prompt.mcp) as client:
|
||||
assert client.server_capabilities is not None
|
||||
assert client.server_capabilities.prompts is not None
|
||||
assert client.initialize_result.capabilities.prompts is not None
|
||||
|
||||
# Test review_code prompt
|
||||
prompts = await client.list_prompts()
|
||||
|
|
@ -223,8 +220,7 @@ async def test_tool_progress() -> None:
|
|||
async def test_sampling() -> None:
|
||||
"""Test sampling (LLM interaction) functionality."""
|
||||
async with Client(sampling.mcp, sampling_callback=sampling_callback) as client:
|
||||
assert client.server_capabilities is not None
|
||||
assert client.server_capabilities.tools is not None
|
||||
assert client.initialize_result.capabilities.tools is not None
|
||||
|
||||
# Test sampling tool
|
||||
sampling_result = await client.call_tool("generate_poem", {"topic": "nature"})
|
||||
|
|
@ -294,9 +290,8 @@ async def test_notifications() -> None:
|
|||
async def test_completion() -> None:
|
||||
"""Test completion (autocomplete) functionality."""
|
||||
async with Client(completion.mcp) as client:
|
||||
assert client.server_capabilities is not None
|
||||
assert client.server_capabilities.resources is not None
|
||||
assert client.server_capabilities.prompts is not None
|
||||
assert client.initialize_result.capabilities.resources is not None
|
||||
assert client.initialize_result.capabilities.prompts is not None
|
||||
|
||||
# Test resource completion
|
||||
completion_result = await client.complete(
|
||||
|
|
|
|||
Loading…
Reference in a new issue