Documentation
¶
Overview ¶
Package mcp implements a Model Context Protocol (MCP) server.
The MCP server exposes Koopa's tool capabilities via the Model Context Protocol, enabling integration with Genkit CLI, Cursor, Claude Code, and other MCP clients. This allows external LLM tools to call Koopa's tools (file operations, system commands, etc.) through a standardized protocol interface.
Overview ¶
MCP is a protocol for exposing tools and resources to language models and AI assistants. The mcp package implements the server-side protocol handler that:
- Accepts MCP protocol requests from clients
- Translates requests to Koopa's internal tool format
- Executes the requested operations
- Returns results in MCP protocol format
Architecture ¶
The MCP server follows a handler-based architecture:
MCP Client (Genkit CLI, Cursor, etc.)
|
| (MCP protocol over stdio/HTTP)
|
v
Server (MCP SDK)
|
+-- Tool Registry (maps tool names to handlers)
|
+-- Handler Chain
| |
| +-- readFile handler
| +-- Other tool handlers (future)
|
v
Toolsets (File, System, Network, etc.)
|
v
Execution Results
Supported Tools ¶
Currently, the MCP server supports:
- readFile: Read file contents with path validation and security checks
Additional tools can be registered by adding handler methods following the pattern established by registerReadFile.
Tool Handler Pattern ¶
Tool handlers follow Go's net/http.Handler pattern for simplicity and consistency:
- Define input schema struct with JSON tags and descriptions
- Infer JSON schema using jsonschema-go
- Create mcp.Tool with name, description, and schema
- Register handler using mcp.AddTool with inline logic
- No conversion functions - build responses directly
This approach keeps code simple and maintainable, following Go conventions.
Security ¶
The MCP server enforces several security measures:
- Path validation: Prevents directory traversal and symlink escapes
- File type restrictions: Only allows text-based files
- Error handling: Sanitizes error messages before returning to clients
- Input validation: Validates all input parameters
Tools like readFile are designed to be safe for untrusted clients by restricting operations to safe directories and file types.
Integration with Genkit CLI ¶
The MCP server can be launched via Genkit CLI's MCP integration:
genkit run -- <your-mcp-server>
This makes all registered tools available within Genkit flows and the CLI interface.
Example Usage ¶
package main
import (
"context"
"github.com/koopa0/koopa/internal/mcp"
"github.com/koopa0/koopa/internal/tools"
"github.com/modelcontextprotocol/go-sdk/mcp"
"os"
)
func main() {
ctx := context.Background()
// Create file toolset
fileToolset := tools.NewFileToolset(nil)
// Create MCP server
server, err := mcp.NewServer(mcp.Config{
Name: "koopa",
Version: "1.0.0",
FileToolset: fileToolset,
})
if err != nil {
panic(err)
}
// Use stdio transport (standard for MCP servers)
transport := mcp.NewStdioServerTransport()
// Run server (blocking)
if err := server.Run(ctx, transport); err != nil {
panic(err)
}
}
Extending with More Tools ¶
To add new tools to the MCP server:
- Create a handler function following the readFile pattern
- Add it to registerTools()
- Use mcp.AddTool to register with the SDK
- Ensure proper error handling and validation
Example structure for a new tool:
type NewToolInput struct {
Param string `json:"param" jsonschema:"Description of param"`
}
func (s *Server) registerNewTool() error {
inputSchema, _ := jsonschema.For[NewToolInput](nil)
tool := &mcp.Tool{
Name: "newTool",
Description: "Description",
InputSchema: inputSchema,
}
mcp.AddTool(s.mcpServer, tool, func(ctx context.Context,
req *mcp.CallToolRequest, in NewToolInput) (*mcp.CallToolResult, any, error) {
// Implementation
})
return nil
}
Error Handling ¶
The MCP server distinguishes between two types of errors:
System errors: Implementation bugs or resource exhaustion Return as MCP protocol error (non-2xx status)
Agent errors: Tool validation failures or user mistakes Return as successful response with error content and IsError=true This allows clients to handle errors gracefully
Thread Safety ¶
The MCP server is safe for concurrent use. The underlying transport and message handling is managed by the MCP SDK.
Index ¶
- type Config
- type Server
- func (s *Server) CurrentTime(ctx context.Context, _ *mcp.CallToolRequest, input tools.CurrentTimeInput) (*mcp.CallToolResult, any, error)
- func (s *Server) DeleteFile(ctx context.Context, _ *mcp.CallToolRequest, input tools.DeleteFileInput) (*mcp.CallToolResult, any, error)
- func (s *Server) ExecuteCommand(ctx context.Context, _ *mcp.CallToolRequest, input tools.ExecuteCommandInput) (*mcp.CallToolResult, any, error)
- func (s *Server) GetEnv(ctx context.Context, _ *mcp.CallToolRequest, input tools.GetEnvInput) (*mcp.CallToolResult, any, error)
- func (s *Server) GetFileInfo(ctx context.Context, _ *mcp.CallToolRequest, input tools.GetFileInfoInput) (*mcp.CallToolResult, any, error)
- func (s *Server) ListFiles(ctx context.Context, _ *mcp.CallToolRequest, input tools.ListFilesInput) (*mcp.CallToolResult, any, error)
- func (s *Server) ReadFile(ctx context.Context, _ *mcp.CallToolRequest, input tools.ReadFileInput) (*mcp.CallToolResult, any, error)
- func (s *Server) Run(ctx context.Context, transport mcp.Transport) error
- func (s *Server) WebFetch(ctx context.Context, _ *mcp.CallToolRequest, input tools.FetchInput) (*mcp.CallToolResult, any, error)
- func (s *Server) WebSearch(ctx context.Context, _ *mcp.CallToolRequest, input tools.SearchInput) (*mcp.CallToolResult, any, error)
- func (s *Server) WriteFile(ctx context.Context, _ *mcp.CallToolRequest, input tools.WriteFileInput) (*mcp.CallToolResult, any, error)
Constants ¶
This section is empty.
Variables ¶
This section is empty.
Functions ¶
This section is empty.
Types ¶
type Config ¶
type Config struct {
Name string
Version string
FileTools *tools.FileTools
SystemTools *tools.SystemTools
NetworkTools *tools.NetworkTools
}
Config holds MCP server configuration.
type Server ¶
type Server struct {
// contains filtered or unexported fields
}
Server wraps the MCP SDK server and Koopa's tool handlers. It exposes Koopa's tools via the Model Context Protocol.
func (*Server) CurrentTime ¶
func (s *Server) CurrentTime(ctx context.Context, _ *mcp.CallToolRequest, input tools.CurrentTimeInput) (*mcp.CallToolResult, any, error)
CurrentTime handles the currentTime MCP tool call.
func (*Server) DeleteFile ¶
func (s *Server) DeleteFile(ctx context.Context, _ *mcp.CallToolRequest, input tools.DeleteFileInput) (*mcp.CallToolResult, any, error)
DeleteFile handles the deleteFile MCP tool call.
func (*Server) ExecuteCommand ¶
func (s *Server) ExecuteCommand(ctx context.Context, _ *mcp.CallToolRequest, input tools.ExecuteCommandInput) (*mcp.CallToolResult, any, error)
ExecuteCommand handles the executeCommand MCP tool call.
func (*Server) GetEnv ¶
func (s *Server) GetEnv(ctx context.Context, _ *mcp.CallToolRequest, input tools.GetEnvInput) (*mcp.CallToolResult, any, error)
GetEnv handles the getEnv MCP tool call.
func (*Server) GetFileInfo ¶
func (s *Server) GetFileInfo(ctx context.Context, _ *mcp.CallToolRequest, input tools.GetFileInfoInput) (*mcp.CallToolResult, any, error)
GetFileInfo handles the getFileInfo MCP tool call.
func (*Server) ListFiles ¶
func (s *Server) ListFiles(ctx context.Context, _ *mcp.CallToolRequest, input tools.ListFilesInput) (*mcp.CallToolResult, any, error)
ListFiles handles the listFiles MCP tool call.
func (*Server) ReadFile ¶
func (s *Server) ReadFile(ctx context.Context, _ *mcp.CallToolRequest, input tools.ReadFileInput) (*mcp.CallToolResult, any, error)
ReadFile handles the readFile MCP tool call.
func (*Server) Run ¶
Run starts the MCP server on the given transport. This is a blocking call that handles all MCP protocol communication.
func (*Server) WebFetch ¶
func (s *Server) WebFetch(ctx context.Context, _ *mcp.CallToolRequest, input tools.FetchInput) (*mcp.CallToolResult, any, error)
WebFetch handles the web_fetch MCP tool call. Architecture: Direct method call (consistent with file.go and system.go).
func (*Server) WebSearch ¶
func (s *Server) WebSearch(ctx context.Context, _ *mcp.CallToolRequest, input tools.SearchInput) (*mcp.CallToolResult, any, error)
WebSearch handles the web_search MCP tool call. Architecture: Direct method call (consistent with file.go and system.go).
func (*Server) WriteFile ¶
func (s *Server) WriteFile(ctx context.Context, _ *mcp.CallToolRequest, input tools.WriteFileInput) (*mcp.CallToolResult, any, error)
WriteFile handles the writeFile MCP tool call.