-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathmcp_server.go
More file actions
81 lines (69 loc) · 1.91 KB
/
mcp_server.go
File metadata and controls
81 lines (69 loc) · 1.91 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
package main
import (
"context"
"encoding/json"
"fmt"
"net/http"
"github.com/modelcontextprotocol/go-sdk/mcp"
)
type SearchArgs struct {
Query string `json:"query"`
Provider string `json:"provider"`
}
func mcpHandler(ctx context.Context, cc *mcp.ServerSession, params *mcp.CallToolParamsFor[SearchArgs]) (*mcp.CallToolResultFor[any], error) {
provider := params.Arguments.Provider
if provider == "" {
if len(config.EnabledProviders) > 0 {
provider = config.EnabledProviders[0]
} else {
return nil, fmt.Errorf("no provider specified and no enabled providers configured")
}
}
results := runQuery(params.Arguments.Query, provider)
jsonResults, err := json.Marshal(results)
if err != nil {
return nil, fmt.Errorf("failed to marshal search results: %w", err)
}
return &mcp.CallToolResultFor[any]{
Content: []mcp.Content{
&mcp.TextContent{
Text: string(jsonResults),
},
},
}, nil
}
func setupMCPServer(mux *http.ServeMux) {
mcpServer := mcp.NewServer("seargo", "0.1.0", nil)
providerNames := make([]any, 0, len(providersMap))
for k := range providersMap {
providerNames = append(providerNames, k)
}
tool := mcp.NewServerTool(
"search",
"Performs a search query across multiple providers.",
mcpHandler,
mcp.Input(
mcp.Property(
"query",
mcp.Description("The search query string"),
mcp.Required(true),
),
mcp.Property(
"provider",
mcp.Description("The single provider to query (e.g., 'duckduckgo', 'brave', 'bing). Defaults to Bing."),
mcp.Enum(providerNames...),
mcp.Required(false),
),
),
)
mcpServer.AddTools(tool)
sseServer := mcp.NewSSEHandler(func(request *http.Request) *mcp.Server {
return mcpServer
})
mux.Handle("/sse", sseServer)
mux.Handle("/message", sseServer)
streamableHttpServer := mcp.NewStreamableHTTPHandler(func(request *http.Request) *mcp.Server {
return mcpServer
}, nil)
mux.Handle("/mcp", streamableHttpServer)
}