2‐Basic Usage - makbn/mcp_mediator GitHub Wiki
All the examples are available under mcp-mediator-exmple
module. It's still a work in progress and the examples will be added.
This example shows how to bootstrap a basic STDIO mediator:
DefaultMcpMediator mediator = new DefaultMcpMediator(McpMediatorConfigurationBuilder.builder()
.createDefault()
.serverName(MY_EXAMPLE_MCP_SERVER_STDIO)
.build());
mediator.registerHandler(new DockerMcpRequestHandler());
mediator.initialize();
Here we:
-
Build a default STDIO configuration with a custom server name and version. [Code]
-
Register a DockerMcpRequestHandler to handle Docker‑related MCP requests. [Code]
-
Call initialize() to start listening on STDIO for incoming JSON‑RPC messages. [Code]
This example demonstrates proxying one or more remote MCP servers over STDIO:
// as an example ~/sdk/jdk/jdk-17.0.14+7/Contents/Home/bin/java
String command = args[0];
// e.g., -jar ~/mcp-mediator-example.jar
List<String> remoteServerArgs = List.of(Arrays.copyOfRange(args, 1, args.length));
ProxyMcpMediator mediator = new ProxyMcpMediator(McpMediatorConfigurationBuilder.builder()
.creatProxy()
.serializer(new ObjectMapper())
.tools(true)
.addRemoteServer(McpMediatorProxyConfiguration.McpMediatorRemoteMcpServerConfiguration.builder()
.remoteTransportType(McpTransportType.STDIO)
.remoteServerAddress(command)
.remoteServerArgs(remoteServerArgs)
.build())
.serverName(MY_EXAMPLE_MCP_SERVER_STDIO)
.serverVersion("1.0.0.0")
.build());
mediator.initialize();
Key steps:
-
Use .creatProxy() to switch to proxy mode and supply a custom ObjectMapper. [Code]
-
Enable tool discovery (.tools(true)) and register one or more remote servers (transport type, command, arguments). [Code]
-
Set a local server name/version and call initialize(), which will forward all proxied tools alongside any local ones. [Code]
ProxyMcpMediator
is basically and extension to DefaultMcpMediator
with added functionallity to proxy requests to remote servers. This means it can handle registered MCP Mediator Request Handlers as well. Below is a comprehensive example demonstrating how to set up a ProxyMcpMediator that integrates a remote MCP server and registers a local handler:
ProxyMcpMediator mediator = new ProxyMcpMediator(McpMediatorConfigurationBuilder.builder()
.creatProxy()
.serializer(new ObjectMapper())
.tools(true)
.addRemoteServer(McpMediatorProxyConfiguration.McpMediatorRemoteMcpServerConfiguration.builder()
.remoteTransportType(McpTransportType.STDIO)
.remoteServerAddress(command)
.remoteServerArgs(remoteServerArgs)
.build())
.serverName(MY_EXAMPLE_MCP_SERVER_STDIO)
.serverVersion("1.0.0.0")
.build());
// extend the proxy server with local tools and services
mediator.registerHandler(new DockerMcpRequestHandler());
mediator.initialize();
Warning
By registering a local handler with the same tool name as one provided by a remote server, you can override the behavior of that remote tool locally. To avoid unintentionally overriding local request handlers, choose unique handler method names carefully. In future development, a configuration point will be introduced to explicitly control the priority of request executors.