gnodet commented on code in PR #26146: URL: https://github.com/apache/camel/pull/26146#discussion_r3951590919
########## components/camel-ai/camel-mcp-server/src/main/java/org/apache/camel/component/mcp/server/stdio/StdioMcpServerEngine.java: ########## @@ -0,0 +1,187 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one or more + * contributor license agreements. See the NOTICE file distributed with + * this work for additional information regarding copyright ownership. + * The ASF licenses this file to You under the Apache License, Version 2.0 + * (the "License"); you may not use this file except in compliance with + * the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +package org.apache.camel.component.mcp.server.stdio; + +import java.io.InputStream; +import java.io.OutputStream; +import java.util.Map; + +import io.modelcontextprotocol.json.McpJsonDefaults; +import io.modelcontextprotocol.json.McpJsonMapper; +import io.modelcontextprotocol.server.McpServer; +import io.modelcontextprotocol.server.McpServerFeatures; +import io.modelcontextprotocol.server.McpSyncServer; +import io.modelcontextprotocol.server.transport.StdioServerTransportProvider; +import io.modelcontextprotocol.spec.McpSchema; +import org.apache.camel.CamelContext; +import org.apache.camel.component.ai.tool.AiToolAnnotations; +import org.apache.camel.component.mcp.server.McpServerEngine; +import org.apache.camel.component.mcp.server.McpServerInfo; +import org.apache.camel.component.mcp.server.McpServerTool; +import org.apache.camel.component.mcp.server.McpToolCallResult; +import org.apache.camel.support.service.ServiceSupport; +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; + +/** + * {@link McpServerEngine} for Camel Main / JBang: serves MCP over the process stdio transport using the official MCP + * Java SDK. Intended for IDE and local agent integration where the parent process launches {@code camel run} as a child + * and speaks JSON-RPC on stdin/stdout. In this mode stdout must carry only MCP protocol frames — callers should route + * logging and startup banners to stderr. + * + * @since 4.23 + */ +public class StdioMcpServerEngine extends ServiceSupport implements McpServerEngine { + + private static final Logger LOG = LoggerFactory.getLogger(StdioMcpServerEngine.class); + + private static final String EMPTY_OBJECT_SCHEMA = """ + { + "type": "object", + "properties": {}, + "additionalProperties": false + } + """; + + private CamelContext camelContext; + private McpServerInfo info; + private McpJsonMapper jsonMapper; + private StdioServerTransportProvider transport; + private McpSyncServer server; + private InputStream inputStream; + private OutputStream outputStream; + + @Override + public CamelContext getCamelContext() { + return camelContext; + } + + @Override + public void setCamelContext(CamelContext camelContext) { + this.camelContext = camelContext; + } + + @Override + public void initialize(McpServerInfo info) { + this.info = info; + } + + /** + * Optional test hook: when both streams are set, the engine uses them instead of {@link System#in} and + * {@link System#out}. + */ + public void setTransportStreams(InputStream inputStream, OutputStream outputStream) { + this.inputStream = inputStream; + this.outputStream = outputStream; + } + + @Override + public boolean consumesServingConfiguration() { + return true; + } + + @Override + protected void doStart() throws Exception { + jsonMapper = McpJsonDefaults.getMapper(); + if (inputStream != null && outputStream != null) { + transport = new StdioServerTransportProvider(jsonMapper, inputStream, outputStream); + } else { + transport = new StdioServerTransportProvider(jsonMapper); + } + server = McpServer.sync(transport) + .serverInfo(info.serverName(), info.version()) + .capabilities(McpSchema.ServerCapabilities.builder().tools(true).build()) + .immediateExecution(true) + .build(); + LOG.info("MCP server '{}' serving ai-tool routes over stdio", info.serverName()); + } + + @Override + protected void doStop() throws Exception { + if (server != null) { + server.closeGracefully(); + server = null; + } + transport = null; Review Comment: ⚠️ **Concern:** `transport` is set to `null` without being explicitly closed. Whether this leaks depends on whether `server.closeGracefully()` internally closes the `StdioServerTransportProvider`. If it does not, the stdin/stdout streams that were passed to the constructor remain open after `doStop()`. Consider closing the transport explicitly before nulling it: ```suggestion transport = null; ``` Actually — if `McpSyncServer.closeGracefully()` closes the underlying transport (which the MCP SDK typically guarantees), leave as-is. If not, add `transport.close()` before `transport = null`. Either way, a brief comment here documenting the assumption would prevent future confusion. ########## components/camel-ai/camel-mcp-server/src/main/java/org/apache/camel/component/mcp/server/stdio/StdioMcpServerEngine.java: ########## @@ -0,0 +1,187 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one or more + * contributor license agreements. See the NOTICE file distributed with + * this work for additional information regarding copyright ownership. + * The ASF licenses this file to You under the Apache License, Version 2.0 + * (the "License"); you may not use this file except in compliance with + * the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +package org.apache.camel.component.mcp.server.stdio; + +import java.io.InputStream; +import java.io.OutputStream; +import java.util.Map; + +import io.modelcontextprotocol.json.McpJsonDefaults; +import io.modelcontextprotocol.json.McpJsonMapper; +import io.modelcontextprotocol.server.McpServer; +import io.modelcontextprotocol.server.McpServerFeatures; +import io.modelcontextprotocol.server.McpSyncServer; +import io.modelcontextprotocol.server.transport.StdioServerTransportProvider; +import io.modelcontextprotocol.spec.McpSchema; +import org.apache.camel.CamelContext; +import org.apache.camel.component.ai.tool.AiToolAnnotations; +import org.apache.camel.component.mcp.server.McpServerEngine; +import org.apache.camel.component.mcp.server.McpServerInfo; +import org.apache.camel.component.mcp.server.McpServerTool; +import org.apache.camel.component.mcp.server.McpToolCallResult; +import org.apache.camel.support.service.ServiceSupport; +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; + +/** + * {@link McpServerEngine} for Camel Main / JBang: serves MCP over the process stdio transport using the official MCP + * Java SDK. Intended for IDE and local agent integration where the parent process launches {@code camel run} as a child + * and speaks JSON-RPC on stdin/stdout. In this mode stdout must carry only MCP protocol frames — callers should route + * logging and startup banners to stderr. + * + * @since 4.23 + */ +public class StdioMcpServerEngine extends ServiceSupport implements McpServerEngine { + + private static final Logger LOG = LoggerFactory.getLogger(StdioMcpServerEngine.class); + + private static final String EMPTY_OBJECT_SCHEMA = """ + { + "type": "object", + "properties": {}, + "additionalProperties": false + } + """; + + private CamelContext camelContext; + private McpServerInfo info; + private McpJsonMapper jsonMapper; + private StdioServerTransportProvider transport; + private McpSyncServer server; + private InputStream inputStream; + private OutputStream outputStream; + + @Override + public CamelContext getCamelContext() { + return camelContext; + } + + @Override + public void setCamelContext(CamelContext camelContext) { + this.camelContext = camelContext; + } + + @Override + public void initialize(McpServerInfo info) { + this.info = info; + } + + /** + * Optional test hook: when both streams are set, the engine uses them instead of {@link System#in} and + * {@link System#out}. + */ + public void setTransportStreams(InputStream inputStream, OutputStream outputStream) { + this.inputStream = inputStream; + this.outputStream = outputStream; + } + + @Override + public boolean consumesServingConfiguration() { + return true; + } + + @Override + protected void doStart() throws Exception { + jsonMapper = McpJsonDefaults.getMapper(); + if (inputStream != null && outputStream != null) { + transport = new StdioServerTransportProvider(jsonMapper, inputStream, outputStream); + } else { + transport = new StdioServerTransportProvider(jsonMapper); + } + server = McpServer.sync(transport) + .serverInfo(info.serverName(), info.version()) + .capabilities(McpSchema.ServerCapabilities.builder().tools(true).build()) + .immediateExecution(true) + .build(); + LOG.info("MCP server '{}' serving ai-tool routes over stdio", info.serverName()); + } + + @Override + protected void doStop() throws Exception { + if (server != null) { + server.closeGracefully(); + server = null; + } + transport = null; + } + + @Override + public void toolAdded(McpServerTool tool) { + McpSchema.Tool mcpTool = buildMcpTool(tool); + McpServerFeatures.SyncToolSpecification spec = McpServerFeatures.SyncToolSpecification.builder() + .tool(mcpTool) + .callHandler((exchange, request) -> { + Map<String, Object> arguments = request.arguments() != null ? request.arguments() : Map.of(); + McpToolCallResult result = tool.handler().call(arguments); + return McpSchema.CallToolResult.builder() + .addTextContent(result.text()) + .isError(result.isError()) + .build(); + }) + .build(); + server.addTool(spec); + LOG.debug("MCP tool added: {}", tool.name()); + } + + @Override + public void toolRemoved(String toolName) { Review Comment: 💡 **Nit:** `server.removeTool(toolName)` will throw `NullPointerException` (not caught as a meaningful `Exception`) if `toolRemoved` is called after `doStop()` sets `server = null`. The catch swallows it silently at `DEBUG` level. This won't happen in normal operation because `McpServerBridge.doStop()` removes the listener before stopping the engine — but a guard would make the defensive intent explicit: ```suggestion public void toolRemoved(String toolName) { if (server == null) { return; } try { server.removeTool(toolName); LOG.debug("MCP tool removed: {}", toolName); } catch (Exception e) { LOG.debug("Failed to remove MCP tool {}: {}", toolName, e.getMessage()); } } ``` ########## components/camel-ai/camel-mcp-server/src/main/docs/mcp-server.adoc: ########## @@ -245,17 +276,30 @@ JBang (see the xref:main.adoc[camel-main] options) or on |=== Bridge-owned options are honored identically on every runtime. Engine-owned -options are consumed by the Vert.x engine only; on runtimes with a native -engine (Quarkus, Spring Boot) the native configuration decides serving -concerns and a startup WARN is logged when an ignored option is set. +options are consumed by the Vert.x HTTP engine or the stdio engine on Camel +Main/JBang; on runtimes with a native engine (Quarkus, Spring Boot) the native +configuration decides serving concerns and a startup WARN is logged when an +ignored option is set. + +On other runtimes, or when wiring programmatically, add the +`McpServerBridge` service to the CamelContext instead: + +[source,java] +---- +McpServerConfiguration configuration = new McpServerConfiguration(); +configuration.setTags("crm,notify"); +camelContext.addService(new McpServerBridge(configuration)); Review Comment: 🔴 **Unresolved from previous review:** The programmatic wiring example is still stripped to only `setTags`. Users who configure `McpServerBridge` programmatically (Quarkus, Spring Boot, custom runtimes) have no reference for `setServerName`, `setServerTitle`, `setServerDescription`, `setServerWebsiteUrl`, `setInstructions`, and `setServerIcons`. Please restore the full example: ```suggestion `McpServerBridge` service to the CamelContext instead: [source,java] ---- McpServerConfiguration configuration = new McpServerConfiguration(); configuration.setTags("crm,notify"); configuration.setServerName("my-integration-app"); configuration.setServerTitle("My Integration MCP"); configuration.setServerDescription("Tools for customer operations"); configuration.setServerWebsiteUrl("https://example.com/docs"); configuration.setInstructions("Use these tools to operate the integration."); configuration.setServerIcons(List.of(new McpServerIcon("https://example.com/icon.png", "image/png", List.of("48x48"), "light"))); camelContext.addService(new McpServerBridge(configuration)); ---- ``` -- This is an automated message from the Apache Git Service. To respond to the message, please log on to GitHub and use the URL above to go to the specific comment. To unsubscribe, e-mail: [email protected] For queries about this service, please contact Infrastructure at: [email protected]
