forked from mark3labs/mcp-go
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmain.go
More file actions
291 lines (253 loc) · 8.49 KB
/
Copy pathmain.go
File metadata and controls
291 lines (253 loc) · 8.49 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
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
//go:build ignore
// +build ignore
// NOTE: This example will not compile until task tool implementation is complete.
// Remove the build constraint above once TAS-4 through TAS-10 are implemented.
package main
import (
"context"
"flag"
"fmt"
"log"
"time"
"github.com/mark3labs/mcp-go/mcp"
"github.com/mark3labs/mcp-go/server"
)
// This example demonstrates task-augmented tools in MCP.
// Task-augmented tools execute asynchronously and return results via polling.
//
// NOTE: This example requires the task tool implementation to be complete.
// Specifically, it requires:
// - TaskToolHandlerFunc type (TAS-4)
// - ServerTaskTool type (TAS-5)
// - AddTaskTool method (TAS-6)
// - handleTaskAugmentedToolCall (TAS-9)
// - executeTaskTool (TAS-10)
//
// The example includes three types of tools:
// 1. process_batch - A TaskSupportRequired tool that processes items in batch
// 2. analyze_data - A TaskSupportOptional tool that can run sync or async
// 3. quick_check - A regular synchronous tool for comparison
//
// Usage:
// go run main.go # Start with stdio transport
// go run main.go -t http # Start with HTTP transport on :8080
func main() {
var transport string
flag.StringVar(&transport, "t", "stdio", "Transport type (stdio or http)")
flag.StringVar(&transport, "transport", "stdio", "Transport type (stdio or http)")
flag.Parse()
mcpServer := NewTaskToolServer()
if transport == "http" {
httpServer := server.NewStreamableHTTPServer(mcpServer)
log.Printf("HTTP server listening on :8080/mcp")
if err := httpServer.Start(":8080"); err != nil {
log.Fatalf("Server error: %v", err)
}
} else {
if err := server.ServeStdio(mcpServer); err != nil {
log.Fatalf("Server error: %v", err)
}
}
}
// NewTaskToolServer creates a new MCP server with task-augmented tool examples.
func NewTaskToolServer() *server.MCPServer {
// Set up task observability hooks
taskHooks := &server.TaskHooks{}
taskHooks.AddOnTaskCreated(func(ctx context.Context, metrics server.TaskMetrics) {
log.Printf("[METRICS] Task created: %s (tool: %s)", metrics.TaskID, metrics.ToolName)
})
taskHooks.AddOnTaskCompleted(func(ctx context.Context, metrics server.TaskMetrics) {
log.Printf("[METRICS] Task completed: %s (tool: %s, duration: %v)",
metrics.TaskID, metrics.ToolName, metrics.Duration)
})
taskHooks.AddOnTaskFailed(func(ctx context.Context, metrics server.TaskMetrics) {
log.Printf("[METRICS] Task failed: %s (tool: %s, duration: %v, error: %v)",
metrics.TaskID, metrics.ToolName, metrics.Duration, metrics.Error)
})
taskHooks.AddOnTaskCancelled(func(ctx context.Context, metrics server.TaskMetrics) {
log.Printf("[METRICS] Task cancelled: %s (tool: %s, duration: %v)",
metrics.TaskID, metrics.ToolName, metrics.Duration)
})
// Create server with task capabilities enabled
// listTasks: allows clients to list all tasks
// cancel: allows clients to cancel running tasks
// toolCallTasks: enables task augmentation for tools
mcpServer := server.NewMCPServer(
"example-servers/task-tool",
"1.0.0",
server.WithTaskCapabilities(true, true, true),
server.WithToolCapabilities(true),
server.WithTaskHooks(taskHooks),
server.WithMaxConcurrentTasks(10), // Limit to 10 concurrent tasks
server.WithLogging(),
)
// Example 1: Task-Required Tool
// This tool MUST be called with task augmentation
processBatchTool := mcp.NewTool("process_batch",
mcp.WithDescription("Process a batch of items asynchronously. This is a long-running operation that must be executed as a task."),
mcp.WithTaskSupport(mcp.TaskSupportRequired),
mcp.WithArray("items",
mcp.Description("Array of items to process"),
mcp.WithStringItems(),
mcp.Required(),
),
mcp.WithNumber("delay",
mcp.Description("Delay in seconds per item (simulates processing time)"),
mcp.DefaultNumber(1.0),
),
)
mcpServer.AddTaskTool(processBatchTool, handleProcessBatch)
// Example 2: Task-Optional Tool
// This tool can be called with or without task augmentation
analyzeDataTool := mcp.NewTool("analyze_data",
mcp.WithDescription("Analyze data. Can run synchronously (fast) or asynchronously (thorough) depending on how it's called."),
mcp.WithTaskSupport(mcp.TaskSupportOptional),
mcp.WithString("data",
mcp.Description("Data to analyze"),
mcp.Required(),
),
mcp.WithBoolean("thorough",
mcp.Description("Whether to perform thorough analysis (slower)"),
mcp.DefaultBool(false),
),
)
mcpServer.AddTaskTool(analyzeDataTool, handleAnalyzeData)
// Example 3: Regular Synchronous Tool (for comparison)
quickCheckTool := mcp.NewTool("quick_check",
mcp.WithDescription("Perform a quick synchronous check. This tool does not support task augmentation."),
mcp.WithString("input",
mcp.Description("Input to check"),
mcp.Required(),
),
)
mcpServer.AddTool(quickCheckTool, handleQuickCheck)
return mcpServer
}
// handleProcessBatch processes items in a batch asynchronously.
// This demonstrates a task-required tool that performs long-running work.
func handleProcessBatch(
ctx context.Context,
request mcp.CallToolRequest,
) (*mcp.CreateTaskResult, error) {
items := request.GetStringSlice("items", []string{})
delay := request.GetFloat("delay", 1.0)
if len(items) == 0 {
return nil, fmt.Errorf("items array cannot be empty")
}
log.Printf("Starting batch processing of %d items with %v second delay per item", len(items), delay)
// Process each item with delay
results := make([]string, 0, len(items))
for i, item := range items {
select {
case <-ctx.Done():
// Task was cancelled
log.Printf("Batch processing cancelled after %d/%d items", i, len(items))
return nil, ctx.Err()
default:
// Simulate processing time
time.Sleep(time.Duration(delay * float64(time.Second)))
result := fmt.Sprintf("Processed '%s' [%d/%d]", item, i+1, len(items))
results = append(results, result)
log.Printf("Item %d/%d: %s", i+1, len(items), item)
}
}
log.Printf("Batch processing completed: %d items", len(items))
// Return the result
return &mcp.CreateTaskResult{
Task: mcp.Task{
// Task fields are managed by the server
},
}, nil
}
// handleAnalyzeData analyzes data either quickly or thoroughly.
// This demonstrates a task-optional tool that adapts based on how it's called.
func handleAnalyzeData(
ctx context.Context,
request mcp.CallToolRequest,
) (*mcp.CreateTaskResult, error) {
data := request.GetString("data", "")
thorough := request.GetBool("thorough", false)
if data == "" {
return nil, fmt.Errorf("data cannot be empty")
}
analysisType := "quick"
duration := 500 * time.Millisecond
if thorough {
analysisType = "thorough"
duration = 3 * time.Second
}
log.Printf("Starting %s analysis of data: %s", analysisType, data)
// Simulate analysis time
select {
case <-ctx.Done():
log.Printf("Analysis cancelled")
return nil, ctx.Err()
case <-time.After(duration):
// Analysis complete
}
// Generate analysis results
wordCount := len(data)
charCount := len([]rune(data))
result := fmt.Sprintf("Analysis complete (%s mode):\n", analysisType)
result += fmt.Sprintf("- Characters: %d\n", charCount)
result += fmt.Sprintf("- Words: %d\n", wordCount)
if thorough {
result += fmt.Sprintf("- Uppercase: %d\n", countUppercase(data))
result += fmt.Sprintf("- Lowercase: %d\n", countLowercase(data))
result += fmt.Sprintf("- Digits: %d\n", countDigits(data))
}
log.Printf("Analysis completed: %s mode, %d characters", analysisType, charCount)
return &mcp.CreateTaskResult{
Task: mcp.Task{
// Task fields are managed by the server
},
}, nil
}
// handleQuickCheck performs a quick synchronous check.
// This demonstrates a regular tool without task support for comparison.
func handleQuickCheck(
ctx context.Context,
request mcp.CallToolRequest,
) (*mcp.CallToolResult, error) {
input := request.GetString("input", "")
if input == "" {
return nil, fmt.Errorf("input cannot be empty")
}
log.Printf("Quick check: %s", input)
return &mcp.CallToolResult{
Content: []mcp.Content{
mcp.TextContent{
Type: "text",
Text: fmt.Sprintf("Quick check passed for: %s (length: %d)", input, len(input)),
},
},
}, nil
}
// Helper functions for thorough analysis
func countUppercase(s string) int {
count := 0
for _, r := range s {
if r >= 'A' && r <= 'Z' {
count++
}
}
return count
}
func countLowercase(s string) int {
count := 0
for _, r := range s {
if r >= 'a' && r <= 'z' {
count++
}
}
return count
}
func countDigits(s string) int {
count := 0
for _, r := range s {
if r >= '0' && r <= '9' {
count++
}
}
return count
}