Package com.tnsai.mcp.hub
Class McpToolBridge
java.lang.Object
com.tnsai.mcp.hub.McpTool
com.tnsai.mcp.hub.McpToolBridge
- All Implemented Interfaces:
AutoCloseable
Dynamic bridge that wraps any MCP server and exposes all its tools.
Unlike extending McpTool for specific tools, this class provides
a generic bridge to any MCP server, automatically discovering and exposing
all available tools.
Usage Examples
Wrap filesystem MCP server
McpToolBridge bridge = McpToolBridge.stdio(
"npx", "-y", "@modelcontextprotocol/server-filesystem", "/tmp"
);
bridge.connect();
// Discover available tools
System.out.println("Available tools: " + bridge.getToolNames());
// Call any tool
String result = bridge.call("read_file", Map.of("path", "/tmp/test.txt"));
Wrap HTTP-based MCP server
McpToolBridge bridge = McpToolBridge.http("https://mcp.example.com/sse");
bridge.connect();
// Get tool schema
McpToolDefinition searchTool = bridge.getTool("search");
System.out.println("Search schema: " + searchTool.getInputSchema());
// Execute tool
String result = bridge.call("search", Map.of("query", "hello world"));
Use with TnsAI agents
// Create bridge
McpToolBridge bridge = McpToolBridge.stdio("npx", "-y", "@mcp/server");
// Convert to TnsAI tools
List<com.tnsai.annotations.Tool> tnsaiTools = bridge.toTnsAITools();
// Register with agent
agent.registerTools(tnsaiTools);
-
Method Summary
Modifier and TypeMethodDescriptionCall a tool with no arguments.Call a tool with a single argument.Call a tool on the MCP server.static McpToolBridgecustom(McpTransport transport, String name, String description) Create a bridge with custom transport.Get the bridge description.getName()Get the bridge name.Get a formatted list of all available tools.static McpToolBridgeCreate a bridge with HTTP/SSE transport.static McpToolBridgeCreate a bridge with HTTP/SSE transport and custom headers.static McpToolBridgehttpWithApiKey(String serverUrl, String apiKey) Create a bridge with HTTP/SSE transport and API key authentication.static McpToolBridgeCreate a bridge with stdio transport.com.tnsai.tools.method.DynamicToolMethodtoDynamicToolMethod(McpToolDefinition mcpTool) Build aDynamicToolMethodfronting a single MCP tool discovered through this bridge.List<com.tnsai.tools.method.DynamicToolMethod> Convert MCP tools to TnsAI tool definitions.Methods inherited from class com.tnsai.mcp.hub.McpTool
close, connect, disconnect, discoverTools, getServerInfo, getTool, getToolNames, isConnected
-
Method Details
-
stdio
Create a bridge with stdio transport.- Parameters:
command- the command to start the MCP server- Returns:
- a new McpToolBridge
-
http
Create a bridge with HTTP/SSE transport.- Parameters:
serverUrl- the MCP server URL- Returns:
- a new McpToolBridge
-
http
Create a bridge with HTTP/SSE transport and custom headers.- Parameters:
serverUrl- the MCP server URLheaders- custom headers (e.g., authentication)- Returns:
- a new McpToolBridge
-
httpWithApiKey
Create a bridge with HTTP/SSE transport and API key authentication.- Parameters:
serverUrl- the MCP server URLapiKey- API key for Bearer authentication- Returns:
- a new McpToolBridge
-
custom
Create a bridge with custom transport.- Parameters:
transport- the transport to usename- the bridge namedescription- the bridge description- Returns:
- a new McpToolBridge
-
call
Call a tool on the MCP server.- Parameters:
toolName- the tool namearguments- the tool arguments- Returns:
- the result string
-
call
Call a tool with no arguments. -
call
Call a tool with a single argument. -
getName
Get the bridge name. -
getDescription
Get the bridge description. -
getToolsDescription
Get a formatted list of all available tools. -
toTnsAITools
Convert MCP tools to TnsAI tool definitions.Creates wrapper tools that can be registered with TnsAI agents. Each MCP tool becomes a separate TnsAI tool.
- Returns:
- list of TnsAI-compatible tools
-
toDynamicToolMethod
Build aDynamicToolMethodfronting a single MCP tool discovered through this bridge. Use directly when the consumer already has a specific MCP tool definition in hand, otherwise prefertoTnsAITools().The MCP 2025-03-26 tool annotations (
McpToolDefinition.getAnnotations()) are mapped onto the corresponding TnsAI tool hints (TNS-641):
Both default toMCP annotation → TnsAI hint MCP annotation TnsAI hint Effect destructiveHint = truerequiresConfirmation = truebrings the tool under the AGENT-V006 approval gate (TNS-556) — a destructive MCP tool registered without a ToolCallFilternow raises the safety warning instead of dispatching unattendedidempotentHint = trueidempotent = trueinforms the retry / fallback layer falsewhen the annotation is absent (the MCP-conservative reading).readOnlyHint/openWorldHintare not mapped — their natural TnsAI counterpart issideEffect, which is not yet modelled onDynamicToolMethod(deferred in TNS-556 until a runtime consumer reads it).- Since:
- 0.7.0 (replaced the
TnsAIToolWrapper implements Tooladapter that retired with theToolinterface); MCP-annotation → safety-hint mapping added in 0.11.1 (TNS-641)
-