Add MCP integration with MCPServer for tool-based interactions
- Introduce `MCPServer` to support connecting to MCP servers via stdio, SSE, or HTTP. - Implement tool fetching, management, and invocation through MCP. - Add `WithMCPServer` method to `ToolBox` for seamless tool integration. - Extend schema package to handle raw JSON schemas for MCP tools. - Update documentation with MCP usage guidelines and examples.
This commit is contained in:
14
toolbox.go
14
toolbox.go
@@ -2,6 +2,7 @@ package llm
|
||||
|
||||
import (
|
||||
"context"
|
||||
"encoding/json"
|
||||
"errors"
|
||||
"fmt"
|
||||
)
|
||||
@@ -11,6 +12,7 @@ import (
|
||||
// the correct parameters.
|
||||
type ToolBox struct {
|
||||
functions map[string]Function
|
||||
mcpServers map[string]*MCPServer // tool name -> MCP server that provides it
|
||||
dontRequireTool bool
|
||||
}
|
||||
|
||||
@@ -91,6 +93,18 @@ var (
|
||||
)
|
||||
|
||||
func (t ToolBox) executeFunction(ctx *Context, functionName string, params string) (any, error) {
|
||||
// Check if this is an MCP tool
|
||||
if server, ok := t.mcpServers[functionName]; ok {
|
||||
var args map[string]any
|
||||
if params != "" {
|
||||
if err := json.Unmarshal([]byte(params), &args); err != nil {
|
||||
return nil, fmt.Errorf("failed to parse MCP tool arguments: %w", err)
|
||||
}
|
||||
}
|
||||
return server.CallTool(ctx, functionName, args)
|
||||
}
|
||||
|
||||
// Regular function
|
||||
f, ok := t.functions[functionName]
|
||||
|
||||
if !ok {
|
||||
|
||||
Reference in New Issue
Block a user