|
| 1 | +package main |
| 2 | + |
| 3 | +import ( |
| 4 | + "context" |
| 5 | + "encoding/json" |
| 6 | + "fmt" |
| 7 | + "log" |
| 8 | + "os" |
| 9 | + |
| 10 | + "github.com/FreePeak/cortex/pkg/server" |
| 11 | + "github.com/FreePeak/cortex/pkg/tools" |
| 12 | + "github.com/FreePeak/cortex/pkg/types" |
| 13 | +) |
| 14 | + |
| 15 | +func main() { |
| 16 | + // Create a logger that writes to stderr |
| 17 | + logger := log.New(os.Stderr, "[array-test] ", log.LstdFlags) |
| 18 | + |
| 19 | + // Create the server |
| 20 | + mcpServer := server.NewMCPServer("Array Parameter Test", "1.0.0", logger) |
| 21 | + |
| 22 | + // Create a tool with array parameter |
| 23 | + arrayTool := tools.NewTool("array_test", |
| 24 | + tools.WithDescription("Test tool with array parameter"), |
| 25 | + tools.WithArray("string_array", |
| 26 | + tools.Description("Array of strings"), |
| 27 | + tools.Required(), |
| 28 | + tools.Items(map[string]interface{}{ |
| 29 | + "type": "string", |
| 30 | + }), |
| 31 | + ), |
| 32 | + tools.WithArray("number_array", |
| 33 | + tools.Description("Array of numbers"), |
| 34 | + tools.Items(map[string]interface{}{ |
| 35 | + "type": "number", |
| 36 | + }), |
| 37 | + ), |
| 38 | + ) |
| 39 | + |
| 40 | + // Add the tool to the server |
| 41 | + ctx := context.Background() |
| 42 | + err := mcpServer.AddTool(ctx, arrayTool, handleArrayTest) |
| 43 | + if err != nil { |
| 44 | + logger.Fatalf("Error adding tool: %v", err) |
| 45 | + } |
| 46 | + |
| 47 | + // Print tool schema for debugging |
| 48 | + printToolSchema(arrayTool) |
| 49 | + |
| 50 | + // Write server status to stderr |
| 51 | + fmt.Fprintf(os.Stderr, "Starting Array Parameter Test Server...\n") |
| 52 | + fmt.Fprintf(os.Stderr, "Send JSON-RPC messages via stdin to interact with the server.\n") |
| 53 | + fmt.Fprintf(os.Stderr, `Try: {"jsonrpc":"2.0","id":1,"method":"tools/call","params":{"name":"array_test","parameters":{"string_array":["a","b","c"]}}}\n`) |
| 54 | + |
| 55 | + // Serve over stdio |
| 56 | + if err := mcpServer.ServeStdio(); err != nil { |
| 57 | + fmt.Fprintf(os.Stderr, "Error: %v\n", err) |
| 58 | + os.Exit(1) |
| 59 | + } |
| 60 | +} |
| 61 | + |
| 62 | +// Handler for the array test tool |
| 63 | +func handleArrayTest(ctx context.Context, request server.ToolCallRequest) (interface{}, error) { |
| 64 | + // Extract the string array parameter |
| 65 | + stringArray, ok := request.Parameters["string_array"].([]interface{}) |
| 66 | + if !ok { |
| 67 | + return nil, fmt.Errorf("missing or invalid 'string_array' parameter") |
| 68 | + } |
| 69 | + |
| 70 | + // Get the optional number array parameter |
| 71 | + var numberArray []interface{} |
| 72 | + if val, ok := request.Parameters["number_array"]; ok { |
| 73 | + numberArray, _ = val.([]interface{}) |
| 74 | + } |
| 75 | + |
| 76 | + // Return the arrays in the response |
| 77 | + return map[string]interface{}{ |
| 78 | + "content": []map[string]interface{}{ |
| 79 | + { |
| 80 | + "type": "text", |
| 81 | + "text": fmt.Sprintf("Received string array: %v\nReceived number array: %v", stringArray, numberArray), |
| 82 | + }, |
| 83 | + }, |
| 84 | + }, nil |
| 85 | +} |
| 86 | + |
| 87 | +// Print the tool schema |
| 88 | +func printToolSchema(tool *types.Tool) { |
| 89 | + schema := map[string]interface{}{ |
| 90 | + "type": "object", |
| 91 | + "properties": make(map[string]interface{}), |
| 92 | + } |
| 93 | + |
| 94 | + for _, param := range tool.Parameters { |
| 95 | + paramSchema := map[string]interface{}{ |
| 96 | + "type": param.Type, |
| 97 | + "description": param.Description, |
| 98 | + } |
| 99 | + |
| 100 | + if param.Type == "array" && param.Items != nil { |
| 101 | + paramSchema["items"] = param.Items |
| 102 | + } |
| 103 | + |
| 104 | + schema["properties"].(map[string]interface{})[param.Name] = paramSchema |
| 105 | + } |
| 106 | + |
| 107 | + schemaJSON, _ := json.MarshalIndent(schema, "", " ") |
| 108 | + fmt.Fprintf(os.Stderr, "Tool schema:\n%s\n", schemaJSON) |
| 109 | +} |
0 commit comments