Class McpTool

java.lang.Object
com.tnsai.mcp.hub.McpTool
All Implemented Interfaces:
AutoCloseable
Direct Known Subclasses:
McpToolBridge

public abstract class McpTool extends Object implements AutoCloseable
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 Details

    • connect

      public void connect() throws IOException
      Connect to the MCP server and initialize the session.
      Throws:
      IOException - if connection fails
    • disconnect

      public void disconnect() throws IOException
      Disconnect from the MCP server.
      Throws:
      IOException - if disconnection fails
    • isConnected

      public boolean isConnected()
      Check if connected to the MCP server.
    • discoverTools

      public List<McpToolDefinition> discoverTools()
      Discover available tools from the MCP server.
      Returns:
      list of discovered tools
    • getTool

      public McpToolDefinition getTool(String toolName)
      Get a discovered tool by name.
      Parameters:
      toolName - the tool name
      Returns:
      the tool definition, or null if not found
    • getToolNames

      public Set<String> getToolNames()
      Get all discovered tool names.
    • getServerInfo

      public McpClient.ServerInfo getServerInfo()
      Get the MCP server info.
    • close

      public void close()
      Clean up resources. Call when done with the tool.
      Specified by:
      close in interface AutoCloseable