Package com.tnsai.mcp.hub
Class McpTool
java.lang.Object
com.tnsai.mcp.hub.McpTool
- All Implemented Interfaces:
AutoCloseable
- Direct Known Subclasses:
McpToolBridge
Base class for tools that wrap MCP (Model Context Protocol) servers.
Provides automatic MCP server connection, tool discovery, and transparent bridging between TnsAI and MCP ecosystems.
Features
- Auto-connect to MCP server on first use
- Discover available tools from MCP server
- Execute MCP tools with automatic serialization
- Connection pooling and lifecycle management
- Tool result caching (optional)
Usage Examples
Stdio-based MCP Server (npx)
@HubTool(name = "filesystem", description = "File system operations via MCP")
public class FileSystemTool extends McpTool {
public FileSystemTool() {
super(new String[]{"npx", "-y", "@modelcontextprotocol/server-filesystem", "/tmp"});
}
@ActionSpec(description = "Read file contents")
public String readFile(String path) {
return callTool("read_file", Map.of("path", path));
}
@ActionSpec(description = "List directory contents")
public String listDirectory(String path) {
return callTool("list_directory", Map.of("path", path));
}
}
HTTP-based MCP Server
@HubTool(name = "remote-mcp", description = "Remote MCP service")
public class RemoteMcpTool extends McpTool {
public RemoteMcpTool() {
super("https://mcp.example.com/sse", Map.of("Authorization", "Bearer xxx"));
}
@ActionSpec(description = "Search documents")
public String search(String query) {
return callTool("search", Map.of("query", query));
}
}
Dynamic Tool Discovery (use McpToolBridge)
// Create a bridge to any 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"));
// Convert to TnsAI tools for agent use
List<Tool> tools = bridge.toTnsAITools();
- See Also:
-
Method Summary
Modifier and TypeMethodDescriptionvoidclose()Clean up resources.voidconnect()Connect to the MCP server and initialize the session.voidDisconnect from the MCP server.Discover available tools from the MCP server.Get the MCP server info.Get a discovered tool by name.Get all discovered tool names.booleanCheck if connected to the MCP server.
-
Method Details
-
connect
Connect to the MCP server and initialize the session.- Throws:
IOException- if connection fails
-
disconnect
Disconnect from the MCP server.- Throws:
IOException- if disconnection fails
-
isConnected
public boolean isConnected()Check if connected to the MCP server. -
discoverTools
Discover available tools from the MCP server.- Returns:
- list of discovered tools
-
getTool
Get a discovered tool by name.- Parameters:
toolName- the tool name- Returns:
- the tool definition, or null if not found
-
getToolNames
Get all discovered tool names. -
getServerInfo
Get the MCP server info. -
close
public void close()Clean up resources. Call when done with the tool.- Specified by:
closein interfaceAutoCloseable
-