-
Notifications
You must be signed in to change notification settings - Fork 863
Expand file tree
/
Copy pathmain.go
More file actions
150 lines (134 loc) · 4 KB
/
Copy pathmain.go
File metadata and controls
150 lines (134 loc) · 4 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
package main
import (
"context"
"fmt"
"log"
"time"
"github.com/mark3labs/mcp-go/mcp"
"github.com/mark3labs/mcp-go/server"
)
func main() {
// Create MCP server with sampling capability
mcpServer := server.NewMCPServer("sampling-http-server", "1.0.0")
// Enable sampling capability
mcpServer.EnableSampling()
// Add a tool that uses sampling to get LLM responses
mcpServer.AddTool(mcp.Tool{
Name: "ask_llm",
Description: "Ask the LLM a question using sampling over HTTP",
InputSchema: mcp.ToolInputSchema{
Type: "object",
Properties: map[string]any{
"question": map[string]any{
"type": "string",
"description": "The question to ask the LLM",
},
"system_prompt": map[string]any{
"type": "string",
"description": "Optional system prompt to provide context",
},
},
Required: []string{"question"},
},
}, func(ctx context.Context, request mcp.CallToolRequest) (*mcp.CallToolResult, error) {
// Extract parameters
question, err := request.RequireString("question")
if err != nil {
return nil, err
}
systemPrompt := request.GetString("system_prompt", "You are a helpful assistant.")
// Create sampling request
samplingRequest := mcp.CreateMessageRequest{
CreateMessageParams: mcp.CreateMessageParams{
Messages: []mcp.SamplingMessage{
{
Role: mcp.RoleUser,
Content: mcp.TextContent{
Type: "text",
Text: question,
},
},
},
SystemPrompt: systemPrompt,
MaxTokens: 1000,
Temperature: 0.7,
},
}
// Request sampling from the client with timeout
samplingCtx, cancel := context.WithTimeout(ctx, 2*time.Minute)
defer cancel()
serverFromCtx := server.ServerFromContext(ctx)
result, err := serverFromCtx.RequestSampling(samplingCtx, samplingRequest)
if err != nil {
return &mcp.CallToolResult{
Content: []mcp.Content{
mcp.TextContent{
Type: "text",
Text: fmt.Sprintf("Error requesting sampling: %v", err),
},
},
IsError: true,
}, nil
}
// Extract response text safely
var responseText string
if textContent, ok := result.Content.(mcp.TextContent); ok {
responseText = textContent.Text
} else {
responseText = fmt.Sprintf("%v", result.Content)
}
// Return the LLM response
return &mcp.CallToolResult{
Content: []mcp.Content{
mcp.TextContent{
Type: "text",
Text: fmt.Sprintf("LLM Response (model: %s): %s", result.Model, responseText),
},
},
}, nil
})
// Add a simple echo tool for testing
mcpServer.AddTool(mcp.Tool{
Name: "echo",
Description: "Echo back the input message",
InputSchema: mcp.ToolInputSchema{
Type: "object",
Properties: map[string]any{
"message": map[string]any{
"type": "string",
"description": "The message to echo back",
},
},
Required: []string{"message"},
},
}, func(ctx context.Context, request mcp.CallToolRequest) (*mcp.CallToolResult, error) {
message := request.GetString("message", "")
return &mcp.CallToolResult{
Content: []mcp.Content{
mcp.TextContent{
Type: "text",
Text: fmt.Sprintf("Echo: %s", message),
},
},
}, nil
})
// Create HTTP server
httpServer := server.NewStreamableHTTPServer(mcpServer)
log.Println("Starting HTTP MCP server with sampling support on :8080")
log.Println("Endpoint: http://localhost:8080/mcp")
log.Println("")
log.Println("This server supports sampling over HTTP transport.")
log.Println("Clients must:")
log.Println("1. Initialize with sampling capability")
log.Println("2. Establish SSE connection for bidirectional communication")
log.Println("3. Handle incoming sampling requests from the server")
log.Println("4. Send responses back via HTTP POST")
log.Println("")
log.Println("Available tools:")
log.Println("- ask_llm: Ask the LLM a question (requires sampling)")
log.Println("- echo: Simple echo tool (no sampling required)")
// Start the server
if err := httpServer.Start(":8080"); err != nil {
log.Fatalf("Server failed to start: %v", err)
}
}