Skip to content
Merged
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
7 changes: 7 additions & 0 deletions changes.md
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,13 @@ Changes log
`Response.redirectPermanentStrict(...)`, `ServerResource.redirectPermanentStrict(...)` and
`Router.redirectPermanentStrict(...)`. The Javadoc of the 301 helpers no longer promises
that the method is kept (#1746).
- `McpClientAdapter` in `auto` mode now probes stdio servers too (#1670), over its own
stream or a `stdio:` connector: it sends `server/discover` within the request timeout and
keeps the 2026-07-28 client when the server names that revision, or falls back to a
legacy `initialize`, offering the legacy revisions, on the same stream when the server
rejects the probe or leaves it unanswered, dropping a late answer to the probe. The
2026-07-28 client, also when asked for explicitly, now speaks over stdio through the SDK's
stdio transport; `getTasksClient()` stays HTTP only.
- `McpServerAdapter.setJsonMapper` and `McpClientAdapter.setJsonMapper` (for the
2026-07-28 client) let the application supply the JSON mapper of the MCP messages, the
SDK's Jackson 3 one by default (#1705).
Expand Down

Large diffs are not rendered by default.

Original file line number Diff line number Diff line change
Expand Up @@ -23,9 +23,9 @@ public final class McpProtocol {
public static final String MODERN_VERSION = "2026-07-28";

/**
* Makes {@link McpClientAdapter} speak whichever revision the server does, experimentally: over
* HTTP, {@link #MODERN_VERSION} when the server answers {@code server/discover}, else one of
* {@link #LEGACY_VERSIONS} through an {@code initialize} handshake.
* Makes {@link McpClientAdapter} speak whichever revision the server does, experimentally, over
* HTTP or stdio: {@link #MODERN_VERSION} when the server answers {@code server/discover}, else
* one of {@link #LEGACY_VERSIONS} through an {@code initialize} handshake.
*/
public static final String AUTO = "auto";

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,24 +8,34 @@
*/
package org.restlet.ext.mcp;

import java.io.IOException;
import java.io.UncheckedIOException;
import java.time.Duration;
import java.util.List;
import org.restlet.Client;
import org.restlet.Request;
import org.restlet.Response;
import org.restlet.data.Protocol;
import org.restlet.engine.connector.ClientHelper;
import org.restlet.ext.mcp.internal.McpLegacyEdge;
import org.restlet.ext.mcp.internal.McpStdioChannel;

/**
* Explicit MCP STDIO connector helper. Its owner supplies an unstarted StreamTransport in the
* connector context under STREAM_TRANSPORT_ATTRIBUTE and stops the connector after the {@link
* McpClientAdapter} using it. No executable is inferred from the stdio URI and no system stream is
* installed by default. The connector speaks MCP {@link McpProtocol#VERSION}, the modern client
* being HTTP only for now.
* installed by default.
*
* <p>The connector frames the stream, one JSON-RPC message per line, for one {@link
* McpClientAdapter} at a time, which speaks MCP {@link McpProtocol#VERSION}, {@link
* McpProtocol#MODERN_VERSION} or, with {@link McpProtocol#AUTO}, probes with {@code
* server/discover} first and falls back to a legacy {@code initialize} on the same stream. The
* first client to use the connector fixes its message limit. Stopping the connector ends the
* stream.
*/
public final class StdioClientHelper extends ClientHelper {
static final String TRANSPORT_ATTRIBUTE = "org.restlet.ext.mcp.clientTransport";
private McpLegacyEdge.StdioTransport transport;
private McpStdioChannel channel;

public StdioClientHelper(Client client) {
super(client);
Expand All @@ -39,27 +49,39 @@ public synchronized void start() {
throw new IllegalStateException(
"MCP STDIO requires an explicitly configured StreamTransport");
}
transport = McpLegacyEdge.stdioTransport(stream);
channel = new McpStdioChannel(stream);
getContext().getAttributes().put(TRANSPORT_ATTRIBUTE, this);
}

/**
* Starts a client on this connector's transport, with an MCP client's limits. One client may
* bind it.
* The framing of this connector's stream, which the client adapter using it attaches to.
*
* @return The channel.
*/
synchronized McpLegacyEdge.Client bind(int maxMessageBytes, int maxCalls, Duration timeout) {
if (transport == null) {
synchronized McpStdioChannel channel() {
if (channel == null) {
throw new IllegalStateException("Start the MCP STDIO client connector first");
}
return McpLegacyEdge.stdio(transport, maxMessageBytes, maxCalls, timeout);
return channel;
}

/**
* Starts a legacy client on this connector's stream, with an MCP client's limits, offering
* {@link McpProtocol#VERSION}.
*/
McpLegacyEdge.Client bind(int maxMessageBytes, int maxCalls, Duration timeout) {
return McpLegacyEdge.stdio(
channel(), List.of(McpProtocol.VERSION), maxMessageBytes, maxCalls, timeout);
}

@Override
public synchronized void stop() {
try {
if (transport != null) {
transport.close(Duration.ofSeconds(30));
if (channel != null) {
channel.close(Duration.ofSeconds(30));
}
} catch (IOException e) {
throw new UncheckedIOException(e);
} finally {
getContext().getAttributes().remove(TRANSPORT_ATTRIBUTE, this);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,8 @@
import io.modelcontextprotocol.json.McpJsonMapper;
import io.modelcontextprotocol.modern.JsonRpc;
import io.modelcontextprotocol.modern.McpSchema;
import java.io.IOException;
import java.io.UncheckedIOException;
import java.time.Duration;
import java.util.List;
import java.util.function.Consumer;
Expand All @@ -20,7 +22,6 @@
import org.restlet.ext.mcp.internal.legacy.McpLegacyClient;
import org.restlet.ext.mcp.internal.legacy.McpLegacyServer;
import org.restlet.ext.mcp.internal.legacy.StdioMcpClientTransport;
import org.restlet.util.StreamTransport;
import reactor.core.publisher.Mono;

/**
Expand Down Expand Up @@ -144,16 +145,6 @@ record HttpOptions(
boolean sessions,
String sessionId) {}

/** The transport a stdio client connector owns, for one legacy client to bind. */
interface StdioTransport {

/** Applies a client's limits, before it connects. */
void setLimits(int maxMessageBytes, int maxConcurrentCalls, Duration timeout);

/** Ends the transport. */
void close(Duration timeout);
}

/**
* Creates the server edge of an adapter.
*
Expand Down Expand Up @@ -190,52 +181,30 @@ static Client http(HttpOptions options) {
}

/**
* Starts a legacy client over a stream it owns.
* Starts a legacy client over a stdio channel, opening it with the client's message limit if
* not yet open. The channel stays open when the client closes: the client detaches from it.
*
* @param stream The stream.
* @param maxMessageBytes The largest message.
* @param maxConcurrentCalls The most exchanges at once.
* @param timeout The request timeout.
* @return The started client.
*/
static Client stdio(
StreamTransport stream, int maxMessageBytes, int maxConcurrentCalls, Duration timeout) {
return McpLegacyClient.stdio(
new StdioMcpClientTransport(stream, maxMessageBytes, maxConcurrentCalls, timeout),
true,
timeout);
}

/**
* Starts a legacy client over the transport a stdio connector owns, which stays open when the
* client closes.
*
* @param transport The connector's transport.
* @param channel The channel.
* @param versions The revisions offered, newest first.
* @param maxMessageBytes The largest message.
* @param maxConcurrentCalls The most exchanges at once.
* @param timeout The request timeout.
* @return The started client.
*/
static Client stdio(
StdioTransport transport,
McpStdioChannel channel,
List<String> versions,
int maxMessageBytes,
int maxConcurrentCalls,
Duration timeout) {
transport.setLimits(maxMessageBytes, maxConcurrentCalls, timeout);
return McpLegacyClient.stdio((StdioMcpClientTransport) transport, false, timeout);
}

/**
* Creates the transport of a stdio client connector.
*
* @param stream The stream.
* @return The transport.
*/
static StdioTransport stdioTransport(StreamTransport stream) {
return new StdioMcpClientTransport(
stream,
org.restlet.ext.mcp.McpProtocol.DEFAULT_MAX_MESSAGE_BYTES,
org.restlet.ext.mcp.McpProtocol.DEFAULT_MAX_CONCURRENT_CALLS,
Duration.ofSeconds(30));
try {
channel.open(maxMessageBytes);
} catch (IOException e) {
throw new UncheckedIOException(e);
}
return McpLegacyClient.stdio(
new StdioMcpClientTransport(
channel, false, versions, maxMessageBytes, maxConcurrentCalls, timeout),
timeout);
}
}
Loading