-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathsdk_mcp_tool.go
More file actions
191 lines (161 loc) · 6.11 KB
/
sdk_mcp_tool.go
File metadata and controls
191 lines (161 loc) · 6.11 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
package codexsdk
import (
"github.com/google/jsonschema-go/jsonschema"
"github.com/modelcontextprotocol/go-sdk/mcp"
internalmcp "github.com/ethpandaops/codex-agent-sdk-go/internal/mcp"
)
// Re-export MCP SDK types for public API.
// These are the official MCP protocol types.
type (
// CallToolResult is the server's response to a tool call.
// Use TextResult, ErrorResult, or ImageResult helpers to create results.
CallToolResult = mcp.CallToolResult
// CallToolRequest is the request passed to tool handlers.
CallToolRequest = mcp.CallToolRequest
// McpContent is the interface for content types in tool results.
McpContent = mcp.Content
// McpTextContent represents text content in a tool result.
McpTextContent = mcp.TextContent
// McpImageContent represents image content in a tool result.
McpImageContent = mcp.ImageContent
// McpAudioContent represents audio content in a tool result.
McpAudioContent = mcp.AudioContent
// McpTool represents an MCP tool definition from the official SDK.
McpTool = mcp.Tool
// McpToolHandler is the function signature for low-level tool handlers.
McpToolHandler = mcp.ToolHandler
// McpToolAnnotations describes optional hints about tool behavior.
// Fields include ReadOnlyHint, DestructiveHint, IdempotentHint,
// OpenWorldHint, and Title.
McpToolAnnotations = mcp.ToolAnnotations
// Schema is a JSON Schema object for tool input validation.
Schema = jsonschema.Schema
)
// SdkMcpToolHandler is the function signature for SdkMcpTool handlers.
// It receives the context and request, and returns the result.
//
// Use ParseArguments to extract input as map[string]any from the request.
// Use TextResult, ErrorResult, or ImageResult helpers to create results.
//
// Example:
//
// func(ctx context.Context, req *codexsdk.CallToolRequest) (*codexsdk.CallToolResult, error) {
// args, err := codexsdk.ParseArguments(req)
// if err != nil {
// return codexsdk.ErrorResult(err.Error()), nil
// }
// a := args["a"].(float64)
// return codexsdk.TextResult(fmt.Sprintf("Result: %v", a)), nil
// }
type SdkMcpToolHandler = mcp.ToolHandler
// SdkMcpToolOption configures an SdkMcpTool during construction.
type SdkMcpToolOption func(*SdkMcpTool)
// WithAnnotations sets MCP tool annotations (hints about tool behavior).
// Annotations describe properties like whether a tool is read-only,
// destructive, idempotent, or operates in an open world.
func WithAnnotations(annotations *mcp.ToolAnnotations) SdkMcpToolOption {
return func(t *SdkMcpTool) {
t.ToolAnnotations = annotations
}
}
// SdkMcpTool represents a tool created with NewSdkMcpTool.
type SdkMcpTool struct {
ToolName string
ToolDescription string
ToolSchema *jsonschema.Schema
ToolHandler SdkMcpToolHandler
ToolAnnotations *mcp.ToolAnnotations
}
// Name returns the tool name.
func (t *SdkMcpTool) Name() string {
return t.ToolName
}
// Description returns the tool description.
func (t *SdkMcpTool) Description() string {
return t.ToolDescription
}
// InputSchema returns the JSON Schema for the tool input.
func (t *SdkMcpTool) InputSchema() *jsonschema.Schema {
return t.ToolSchema
}
// Handler returns the tool handler function.
func (t *SdkMcpTool) Handler() SdkMcpToolHandler {
return t.ToolHandler
}
// Annotations returns the tool annotations, or nil if not set.
func (t *SdkMcpTool) Annotations() *mcp.ToolAnnotations {
return t.ToolAnnotations
}
// NewSdkMcpTool creates an SdkMcpTool with optional configuration.
//
// The inputSchema should be a *jsonschema.Schema. Use SimpleSchema for convenience
// or create a full Schema struct for more control.
//
// Use WithAnnotations to set MCP tool annotations (hints about tool behavior).
//
// Example with SimpleSchema:
//
// addTool := codexsdk.NewSdkMcpTool("add", "Add two numbers",
// codexsdk.SimpleSchema(map[string]string{"a": "float64", "b": "float64"}),
// func(ctx context.Context, req *codexsdk.CallToolRequest) (*codexsdk.CallToolResult, error) {
// args, _ := codexsdk.ParseArguments(req)
// a, b := args["a"].(float64), args["b"].(float64)
// return codexsdk.TextResult(fmt.Sprintf("Result: %v", a+b)), nil
// },
// codexsdk.WithAnnotations(&codexsdk.McpToolAnnotations{
// ReadOnlyHint: true,
// }),
// )
func NewSdkMcpTool(
name, description string,
inputSchema *jsonschema.Schema,
handler SdkMcpToolHandler,
opts ...SdkMcpToolOption,
) *SdkMcpTool {
t := &SdkMcpTool{
ToolName: name,
ToolDescription: description,
ToolSchema: inputSchema,
ToolHandler: handler,
}
for _, opt := range opts {
opt(t)
}
return t
}
// SimpleSchema creates a jsonschema.Schema from a simple type map.
//
// Input format: {"a": "float64", "b": "string"}
//
// Type mappings:
// - "string" -> {"type": "string"}
// - "int", "int64" -> {"type": "integer"}
// - "float64", "float" -> {"type": "number"}
// - "bool" -> {"type": "boolean"}
// - "[]string" -> {"type": "array", "items": {"type": "string"}}
// - "any", "object" -> {"type": "object"}
func SimpleSchema(props map[string]string) *jsonschema.Schema {
return internalmcp.SimpleSchema(props)
}
// TextResult creates a CallToolResult with text content.
func TextResult(text string) *mcp.CallToolResult {
return internalmcp.TextResult(text)
}
// ErrorResult creates a CallToolResult indicating an error.
func ErrorResult(message string) *mcp.CallToolResult {
return internalmcp.ErrorResult(message)
}
// ImageResult creates a CallToolResult with image content.
func ImageResult(data []byte, mimeType string) *mcp.CallToolResult {
return internalmcp.ImageResult(data, mimeType)
}
// ParseArguments unmarshals CallToolRequest arguments into a map.
// This is a convenience function for extracting tool input.
func ParseArguments(req *mcp.CallToolRequest) (map[string]any, error) {
return internalmcp.ParseArguments(req)
}
// NewMcpTool creates an mcp.Tool with the given parameters.
// This is useful when you need direct access to the MCP Tool type.
func NewMcpTool(name, description string, inputSchema *jsonschema.Schema) *mcp.Tool {
return internalmcp.NewTool(name, description, inputSchema)
}