Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
23 changes: 23 additions & 0 deletions src/mcp/server/auth/handlers/register.py
Original file line number Diff line number Diff line change
Expand Up @@ -117,6 +117,29 @@ async def handle(self, request: Request) -> Response:
status_code=400,
)

# RFC 7591 §2: "Clients using flows with redirection MUST register their
# redirection URI values." This handler only accepts the authorization_code
# grant, which is such a flow, so a registration without redirect URIs can
# never be used: every /authorize call would fail
# `validate_redirect_uri()` with `InvalidRedirectUriError`. Refuse it here,
# before minting credentials, rather than persisting a dead client.
#
# `redirect_uris` is `list[AnyUrl] | None` on `OAuthClientMetadata` because
# non-redirect clients (the client_credentials and identity-assertion
# extensions) build it with `redirect_uris=None`. `None` satisfies that union
# and bypasses `min_length=1`, so an explicit JSON `"redirect_uris": null`
# parses - and would otherwise register successfully. Omitted and `[]` are
# already rejected by the model; this closes the third spelling, matching the
# check `private_key_jwt` above.
if not client_metadata.redirect_uris:
return PydanticJSONResponse(
content=RegistrationErrorResponse(
error="invalid_client_metadata",
error_description="redirect_uris is required for authorization_code grant",
),
status_code=400,
)

client_id_issued_at = int(time.time())
# RFC 7591 §3.2.1: client_secret_expires_at is REQUIRED whenever a client_secret is
# issued, with 0 (not omission) meaning it never expires; a public client gets none.
Expand Down
119 changes: 118 additions & 1 deletion src/mcp/server/stdio.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,23 +11,29 @@ async def run_server():
```
"""

import json
import logging
import os
import sys
import threading
from collections.abc import Callable
from contextlib import asynccontextmanager, suppress
from dataclasses import dataclass
from io import TextIOWrapper
from typing import BinaryIO, Literal, TextIO
from typing import Any, BinaryIO, Final, Literal, TextIO, cast

import anyio
import anyio.lowlevel
import mcp_types as types
import pydantic_core

from mcp.os.win32.utilities import rebind_std_handle_to_fd
from mcp.shared._context_streams import create_context_streams
from mcp.shared._stream_protocols import WriteStream
from mcp.shared.message import SessionMessage

logger = logging.getLogger(__name__)

if sys.platform != "win32": # pragma: no branch
import fcntl # pragma: lax no cover - POSIX-only line, uncovered on Windows runners

Expand Down Expand Up @@ -158,6 +164,114 @@ def release() -> None:
return os.fdopen(private_fd, mode, closefd=False), release


_PARSE_ERROR_MESSAGE: Final = "Parse error"
"""JSON-RPC 2.0 §5 wording for -32700: the line was not well-formed JSON."""

_INVALID_REQUEST_MESSAGE: Final = "Invalid Request"
"""JSON-RPC 2.0 §5 wording for -32600: well-formed JSON that is not one usable message."""


def _lenient_decode(line: str) -> Any:
"""Decode a line with Python's `json`, which tolerates what pydantic-core refuses.

Used only to *identify* a line pydantic-core rejected, never to accept one:
the engines disagree on invalid Unicode escapes. A lone surrogate such as
`\\ud83d` - the escape `json.dumps(ensure_ascii=True)` writes for half of a
sliced emoji - decodes under `json` and is refused by pydantic-core, so
`json` still yields the envelope, and with it the id the client awaits.

Returns the decoded value, or `None` when even `json` cannot parse the line.
"""
try:
return json.loads(line)
except (ValueError, RecursionError):
# Not just JSONDecodeError: oversized integer literals raise bare
# ValueError and deep nesting raises RecursionError.
return None


def _recover_request_id(line: str) -> types.RequestId | None:
"""The request id of a rejected line, when it carries one the wire accepts.

JSON-RPC 2.0 §5: an error response carries the id of the request it answers,
and `null` only when the id could not be determined. A line that *validated*
badly usually still identifies itself, and answering with `null` would leave
the client's pending call unmatched - precisely the hang being fixed, since
a `null` id correlates with nothing in a client's pending map.

Only the envelope's own top-level `id` counts: an `id` nested inside `params`
is a tool argument, and attributing the reply to it would resolve an
unrelated request. A member that is not a valid `RequestId` (a float, a bool,
an array, an object, or an explicit `null`) is dropped rather than echoed,
because putting it on the wire would violate the same spec.
"""
decoded = _lenient_decode(line)
if not isinstance(decoded, dict):
return None
envelope = cast("dict[str, Any]", decoded)
raw_id: Any = envelope.get("id")
# `bool` subclasses `int`; JSON `true` is not a request id.
if isinstance(raw_id, bool):
return None
if isinstance(raw_id, str):
# The lenient decoder tolerates a string id carrying the same lone
# surrogate that got the line rejected; echoing it would make the reply
# itself unserializable and take the writer down. Such an id is
# undeterminable for this server, so it is answered with `null`.
try:
raw_id.encode("utf-8")
except UnicodeEncodeError:
return None
return raw_id
if isinstance(raw_id, int):
return raw_id
return None


def _invalid_line_response(line: str) -> types.JSONRPCError:
"""The JSON-RPC error owed to a client whose request line could not be accepted.

The code follows the rung the line failed at, which is read by replaying only
the decode stage: `-32700 Parse error` when the JSON layer refuses the line,
`-32600 Invalid Request` when the JSON is well-formed but not one message.
(`jsonrpc_message_adapter` is a union of the four message models, so a batch,
a bare scalar, `{}`, a bad `jsonrpc` version or a missing `method` all fail
the envelope rung rather than the JSON one.)
"""
try:
pydantic_core.from_json(line)
except ValueError:
code, message = types.PARSE_ERROR, _PARSE_ERROR_MESSAGE
else:
code, message = types.INVALID_REQUEST, _INVALID_REQUEST_MESSAGE
return types.JSONRPCError(
jsonrpc="2.0",
id=_recover_request_id(line),
error=types.ErrorData(code=code, message=message),
)


async def _reply_to_invalid_line(write_stream: WriteStream[SessionMessage], line: str, exc: Exception) -> None:
"""Send the error reply for an unusable line instead of leaving the client to time out.

`JSONRPCDispatcher._dispatch` drops a bare `Exception` item when no
`on_stream_exception` observer is installed, and a server session installs
none - so forwarding the exception alone, as this reader used to, produced
silence on the wire. The exception is still forwarded (observers keep seeing
it); the reply is what unblocks the caller.
"""
response = _invalid_line_response(line)
# Warning, not debug: the line is discarded, and that is worth an operator's
# attention - the issue reported the silence on stderr as part of the defect.
logger.warning("rejected stdin line as %s (%s)", response.error.message, type(exc).__name__)
try:
await write_stream.send(SessionMessage(response))
except (anyio.BrokenResourceError, anyio.ClosedResourceError):
# The session already tore down; a reply that was never written must not
# take the reader's task group with it.
logger.debug("dropped %s reply: write stream closed", response.error.code)


@asynccontextmanager
async def stdio_server(stdin: anyio.AsyncFile[str] | None = None, stdout: anyio.AsyncFile[str] | None = None):
"""Serve MCP over the process's stdin and stdout.
Expand Down Expand Up @@ -188,6 +302,9 @@ async def stdin_reader():
try:
message = types.jsonrpc_message_adapter.validate_json(line, by_name=False)
except Exception as exc:
# Forward the fault to observers, and answer the client:
# a dropped line leaves its call pending until timeout.
await _reply_to_invalid_line(write_stream, line, exc)
await read_stream_writer.send(exc)
continue

Expand Down
63 changes: 63 additions & 0 deletions tests/interaction/auth/test_as_handlers.py
Original file line number Diff line number Diff line change
Expand Up @@ -256,6 +256,69 @@ async def test_registration_with_invalid_metadata_is_rejected_with_400(
)


@requirement("hosting:auth:as:register-error-response")
@pytest.mark.parametrize(
("label", "payload"),
[
# RFC 7591 §2 makes redirect_uris REQUIRED for redirection flows; the model
# rejects the three spellings below at parse time.
("omitted", {}),
("empty-array", {"redirect_uris": []}),
# The one the model lets through: `list[AnyUrl] | None` is there so non-redirect
# clients (client_credentials, identity assertion) can build the model with None,
# and None satisfies the union, bypassing min_length=1 (issue #3885).
("explicit-null", {"redirect_uris": None}),
],
ids=["omitted", "empty-array", "explicit-null"],
)
async def test_registration_without_redirect_uris_is_rejected_with_400(
as_app: tuple[httpx2.AsyncClient, InMemoryAuthorizationServerProvider],
label: str,
payload: dict[str, object],
) -> None:
"""Registration without usable redirect URIs is refused, and nothing is persisted.

Regression lock for issue #3885: `{"redirect_uris": null}` returned `201 Created`,
minting a client_id/client_secret and storing a client whose every `/authorize`
request would then fail `validate_redirect_uri()` with `InvalidRedirectUriError`.
RFC 7591 §2: "Clients using flows with redirection MUST register their redirection
URI values"; this handler only accepts the authorization_code grant.
"""
http, provider = as_app
registered_before = len(provider.clients)

response = await http.post("/register", json=payload | {"client_name": label})

assert response.status_code == 400, label
body = response.json()
assert body["error"] == "invalid_client_metadata", label
# The description is the model's for the two it rejects itself, and the handler's
# own for the null spelling; either way the defect is named.
assert "redirect_uris" in body["error_description"], label
# No credentials minted, nothing stored: the refusal is total, not cosmetic.
assert "client_id" not in body, label
assert len(provider.clients) == registered_before, label


@requirement("hosting:auth:as:register-error-response")
async def test_registration_with_redirect_uris_still_succeeds(
as_app: tuple[httpx2.AsyncClient, InMemoryAuthorizationServerProvider],
) -> None:
"""The new null check does not narrow the accepted path: a normal registration still 201s.

Guards against over-rejecting - `redirect_uris` present and non-empty must keep
working, and the stored client must be able to resolve its redirect.
"""
http, provider = as_app

response = await http.post("/register", json=oauth_client_metadata().model_dump(mode="json", exclude_none=True))

assert response.status_code == 201
client_info = OAuthClientInformationFull.model_validate_json(response.content)
assert client_info.redirect_uris
assert provider.clients[client_info.client_id].redirect_uris == client_info.redirect_uris


@requirement("hosting:auth:as:register-echo")
@pytest.mark.parametrize("application_type", ["web", "native"])
async def test_registration_response_echoes_the_registered_application_type(
Expand Down
Loading
Loading