Fix unknown-method error code and add a protocol version registry (#2836)
This commit is contained in:
parent
1e21814ed5
commit
7267818e44
1 changed files with 430 additions and 23 deletions
|
|
@ -168,6 +168,45 @@ async def test_runner_gates_requests_before_initialize(server: SrvT):
|
|||
assert await client.send_raw_request("ping", None) == {}
|
||||
|
||||
|
||||
@pytest.mark.anyio
|
||||
async def test_runner_unknown_method_before_initialize_raises_method_not_found(server: SrvT):
|
||||
"""An unknown method is METHOD_NOT_FOUND even before initialize: JSON-RPC
|
||||
2.0 reserves -32601 for it, and clients probing a server before the
|
||||
handshake key off that code. The init gate only applies to methods the
|
||||
server actually serves."""
|
||||
async with connected_runner(server, initialized=False) as (client, _):
|
||||
with pytest.raises(MCPError) as exc:
|
||||
await client.send_raw_request("x/unknown", None)
|
||||
assert exc.value.error == ErrorData(code=METHOD_NOT_FOUND, message="Method not found", data="x/unknown")
|
||||
|
||||
|
||||
@pytest.mark.anyio
|
||||
async def test_runner_spec_method_without_handler_before_initialize_raises_method_not_found(server: SrvT):
|
||||
"""A spec method the server doesn't serve is METHOD_NOT_FOUND even before
|
||||
initialize: -32601 means "not available on this server", so probing
|
||||
clients get the same answer in every initialization state (the fixture
|
||||
server registers no resources handlers)."""
|
||||
async with connected_runner(server, initialized=False) as (client, _):
|
||||
with pytest.raises(MCPError) as exc:
|
||||
await client.send_raw_request("resources/list", None)
|
||||
assert exc.value.error == ErrorData(code=METHOD_NOT_FOUND, message="Method not found", data="resources/list")
|
||||
|
||||
|
||||
@pytest.mark.anyio
|
||||
async def test_runner_custom_method_with_handler_is_still_gated_before_initialize(server: SrvT):
|
||||
"""A custom-registered method is a known method: before initialize it is
|
||||
rejected by the init gate, not answered with METHOD_NOT_FOUND."""
|
||||
|
||||
async def greet(ctx: Ctx, params: RequestParams | None) -> Any:
|
||||
raise NotImplementedError # the gate rejects the request first
|
||||
|
||||
server.add_request_handler("custom/greet", RequestParams, greet)
|
||||
async with connected_runner(server, initialized=False) as (client, _):
|
||||
with pytest.raises(MCPError) as exc:
|
||||
await client.send_raw_request("custom/greet", None)
|
||||
assert exc.value.error == ErrorData(code=INVALID_PARAMS, message="Invalid request parameters", data="")
|
||||
|
||||
|
||||
@pytest.mark.anyio
|
||||
async def test_runner_routes_to_handler_and_builds_context(server: SrvT):
|
||||
async with connected_runner(server) as (client, runner):
|
||||
|
|
@ -186,7 +225,7 @@ async def test_runner_spec_method_with_no_handler_raises_method_not_found(server
|
|||
async with connected_runner(server) as (client, _):
|
||||
with pytest.raises(MCPError) as exc:
|
||||
await client.send_raw_request("resources/list", None)
|
||||
assert exc.value.error.code == METHOD_NOT_FOUND
|
||||
assert exc.value.error == ErrorData(code=METHOD_NOT_FOUND, message="Method not found", data="resources/list")
|
||||
|
||||
|
||||
@pytest.mark.anyio
|
||||
|
|
@ -196,7 +235,7 @@ async def test_runner_non_spec_method_with_no_handler_raises_method_not_found(se
|
|||
async with connected_runner(server) as (client, _):
|
||||
with pytest.raises(MCPError) as exc:
|
||||
await client.send_raw_request("nonexistent/method", None)
|
||||
assert exc.value.error.code == METHOD_NOT_FOUND
|
||||
assert exc.value.error == ErrorData(code=METHOD_NOT_FOUND, message="Method not found", data="nonexistent/method")
|
||||
|
||||
|
||||
@pytest.mark.anyio
|
||||
|
|
|
|||
Loading…
Reference in a new issue