|
| 1 | +// Example: Setting up a variant-aware MCP server with multiple variants. |
| 2 | +package main |
| 3 | + |
| 4 | +import ( |
| 5 | + "context" |
| 6 | + "log" |
| 7 | + |
| 8 | + mcp "github.com/modelcontextprotocol/go-sdk/mcp" |
| 9 | + |
| 10 | + "github.com/modelcontextprotocol/experimental-ext-variants/go/sdk/variants" |
| 11 | +) |
| 12 | + |
| 13 | +// -- Coding variant tool types ------------------------------------------------ |
| 14 | + |
| 15 | +type AnalyzeCodeInput struct { |
| 16 | + Code string `json:"code" jsonschema:"source code to analyze"` |
| 17 | + Language string `json:"language" jsonschema:"programming language"` |
| 18 | +} |
| 19 | + |
| 20 | +type AnalyzeCodeOutput struct { |
| 21 | + Issues []string `json:"issues"` |
| 22 | + Suggestions []string `json:"suggestions"` |
| 23 | +} |
| 24 | + |
| 25 | +func AnalyzeCode(_ context.Context, _ *mcp.CallToolRequest, in AnalyzeCodeInput) (*mcp.CallToolResult, AnalyzeCodeOutput, error) { |
| 26 | + return nil, AnalyzeCodeOutput{ |
| 27 | + Issues: []string{"unused variable on line 3"}, |
| 28 | + Suggestions: []string{"consider using a switch statement"}, |
| 29 | + }, nil |
| 30 | +} |
| 31 | + |
| 32 | +type RefactorInput struct { |
| 33 | + Code string `json:"code" jsonschema:"source code to refactor"` |
| 34 | + Action string `json:"action" jsonschema:"refactoring action, e.g. extract-function"` |
| 35 | +} |
| 36 | + |
| 37 | +type RefactorOutput struct { |
| 38 | + Refactored string `json:"refactored"` |
| 39 | +} |
| 40 | + |
| 41 | +func Refactor(_ context.Context, _ *mcp.CallToolRequest, in RefactorInput) (*mcp.CallToolResult, RefactorOutput, error) { |
| 42 | + return nil, RefactorOutput{Refactored: "// refactored\n" + in.Code}, nil |
| 43 | +} |
| 44 | + |
| 45 | +// -- Compact variant tool types ----------------------------------------------- |
| 46 | + |
| 47 | +type SummarizeInput struct { |
| 48 | + Text string `json:"text" jsonschema:"text to summarize"` |
| 49 | +} |
| 50 | + |
| 51 | +type SummarizeOutput struct { |
| 52 | + Summary string `json:"summary"` |
| 53 | +} |
| 54 | + |
| 55 | +func Summarize(_ context.Context, _ *mcp.CallToolRequest, in SummarizeInput) (*mcp.CallToolResult, SummarizeOutput, error) { |
| 56 | + return nil, SummarizeOutput{Summary: in.Text[:min(len(in.Text), 50)]}, nil |
| 57 | +} |
| 58 | + |
| 59 | +type LookupInput struct { |
| 60 | + Query string `json:"query" jsonschema:"lookup query"` |
| 61 | +} |
| 62 | + |
| 63 | +type LookupOutput struct { |
| 64 | + Result string `json:"result"` |
| 65 | +} |
| 66 | + |
| 67 | +func Lookup(_ context.Context, _ *mcp.CallToolRequest, in LookupInput) (*mcp.CallToolResult, LookupOutput, error) { |
| 68 | + return nil, LookupOutput{Result: "result for: " + in.Query}, nil |
| 69 | +} |
| 70 | + |
| 71 | +// ----------------------------------------------------------------------------- |
| 72 | + |
| 73 | +func main() { |
| 74 | + // Server 1: coding-focused tools |
| 75 | + server1 := mcp.NewServer(&mcp.Implementation{Name: "my-server", Version: "v1.0.0"}, nil) |
| 76 | + mcp.AddTool(server1, &mcp.Tool{Name: "analyze_code", Description: "Perform static analysis on source code"}, AnalyzeCode) |
| 77 | + mcp.AddTool(server1, &mcp.Tool{Name: "refactor", Description: "Apply a refactoring action to source code"}, Refactor) |
| 78 | + |
| 79 | + // Server 2: compact / minimal tools |
| 80 | + server2 := mcp.NewServer(&mcp.Implementation{Name: "my-server", Version: "v1.0.0"}, nil) |
| 81 | + mcp.AddTool(server2, &mcp.Tool{Name: "summarize", Description: "Summarize text"}, Summarize) |
| 82 | + mcp.AddTool(server2, &mcp.Tool{Name: "lookup", Description: "Quick fact lookup"}, Lookup) |
| 83 | + |
| 84 | + coding := variants.ServerVariant{ |
| 85 | + ID: "coding-assistant", |
| 86 | + Description: "Optimized for coding workflows", |
| 87 | + Hints: map[string]string{"useCase": "coding", "contextSize": "standard"}, |
| 88 | + Status: variants.Stable, |
| 89 | + } |
| 90 | + |
| 91 | + compact := variants.ServerVariant{ |
| 92 | + ID: "compact", |
| 93 | + Description: "Minimal token usage", |
| 94 | + Hints: map[string]string{"contextSize": "small"}, |
| 95 | + Status: variants.Experimental, |
| 96 | + } |
| 97 | + |
| 98 | + vs := variants.NewServer(). |
| 99 | + WithVariant(coding, server1, 0). |
| 100 | + WithVariant(compact, server2, 1). |
| 101 | + WithRanking(func(_ context.Context, _ variants.VariantHints, vs []variants.ServerVariant) []variants.ServerVariant { |
| 102 | + // rank based on client hints, return sorted |
| 103 | + // note: this is optional, defults to ranking by priority. |
| 104 | + return vs |
| 105 | + }) |
| 106 | + |
| 107 | + ctx := context.Background() |
| 108 | + if err := vs.Run(ctx, nil); err != nil { |
| 109 | + log.Fatal(err) |
| 110 | + } |
| 111 | +} |
0 commit comments