fix(session): return METHOD_NOT_FOUND for unknown request methods
Summary
Return JSON-RPC -32601 METHOD_NOT_FOUND for request methods that are not part of the session's request schema, while preserving -32602 INVALID_PARAMS for known methods with invalid parameters. Error responses echo the request id unchanged.
Why
BaseSession previously mapped every request-model validation failure to -32602. That made an unsupported method indistinguishable from a malformed call and caused clients to retry requests that could never succeed.
Implementation
- Derive the supported request-method set from the configured Pydantic request type once per session.
- Apply the distinction centrally in
BaseSession, covering both client and server sessions and custom request unions. - Fail closed if schema introspection cannot produce a method set.
Behavior note
Validation-error responses no longer include the empty data="" field (-32602 and the new -32601 paths both omit it). This is spec-legal — the JSON-RPC data member is optional and its type is implementation-defined — and matches the TypeScript SDK, but clients inspecting error.data may notice the change.
Tests
- Symmetric regression coverage for unknown methods and malformed known methods on both client and server sessions.
- Unknown notifications are verified to produce no response, per JSON-RPC requirements.
- Direct assertion that
_extract_known_request_methodsextracts the full expected method set forClientRequest/ServerRequest, plus fallback behavior for non-string defaults and introspection failures.
Validation
- Focused session tests pass (
tests/shared/test_session.py,tests/server/test_session.py). - Ruff format and Ruff lint pass.
- Behavior verified against the earlier review comments on this PR.