|
| 1 | +package main |
| 2 | + |
| 3 | +import ( |
| 4 | + "context" |
| 5 | + "embed" |
| 6 | + "encoding/json" |
| 7 | + "fmt" |
| 8 | + "log" |
| 9 | + "os" |
| 10 | + |
| 11 | + "github.com/mark3labs/mcp-go/mcp" |
| 12 | + "github.com/mark3labs/mcp-go/server" |
| 13 | + |
| 14 | + "github.com/mercari/grpc-federation/source" |
| 15 | + "github.com/mercari/grpc-federation/validator" |
| 16 | +) |
| 17 | + |
| 18 | +//go:embed assets/* |
| 19 | +var assets embed.FS |
| 20 | + |
| 21 | +func main() { |
| 22 | + docs, err := assets.ReadFile("assets/docs.md") |
| 23 | + if err != nil { |
| 24 | + log.Fatalf("failed to read document: %v", err) |
| 25 | + } |
| 26 | + |
| 27 | + // Create a new MCP server |
| 28 | + s := server.NewMCPServer( |
| 29 | + "gRPC Federation MCP Server", |
| 30 | + "1.0.0", |
| 31 | + server.WithToolCapabilities(false), |
| 32 | + server.WithResourceCapabilities(false, false), |
| 33 | + server.WithRecovery(), |
| 34 | + server.WithInstructions(string(docs)), |
| 35 | + ) |
| 36 | + |
| 37 | + s.AddTool( |
| 38 | + mcp.NewTool( |
| 39 | + "get_import_proto_list", |
| 40 | + mcp.WithDescription("Returns a list of import proto files used in the specified proto file"), |
| 41 | + mcp.WithString( |
| 42 | + "path", |
| 43 | + mcp.Description("The absolute path to the proto file to be analyzed"), |
| 44 | + mcp.Required(), |
| 45 | + ), |
| 46 | + ), |
| 47 | + func(ctx context.Context, r mcp.CallToolRequest) (*mcp.CallToolResult, error) { |
| 48 | + args, ok := r.Params.Arguments.(map[string]any) |
| 49 | + if !ok { |
| 50 | + return nil, fmt.Errorf("unexpected argument format: %T", r.Params.Arguments) |
| 51 | + } |
| 52 | + path, ok := args["path"].(string) |
| 53 | + if !ok { |
| 54 | + return nil, fmt.Errorf("failed to find path parameter from arguments: %v", args) |
| 55 | + } |
| 56 | + content, err := os.ReadFile(path) |
| 57 | + if err != nil { |
| 58 | + return nil, fmt.Errorf("failed to read file: %w", err) |
| 59 | + } |
| 60 | + f, err := source.NewFile(path, content) |
| 61 | + if err != nil { |
| 62 | + return nil, err |
| 63 | + } |
| 64 | + list, err := json.Marshal(append(f.Imports(), f.ImportsByImportRule()...)) |
| 65 | + if err != nil { |
| 66 | + return nil, err |
| 67 | + } |
| 68 | + return &mcp.CallToolResult{ |
| 69 | + Content: []mcp.Content{ |
| 70 | + mcp.TextContent{ |
| 71 | + Type: "text", |
| 72 | + Text: string(list), |
| 73 | + }, |
| 74 | + }, |
| 75 | + }, nil |
| 76 | + }, |
| 77 | + ) |
| 78 | + |
| 79 | + s.AddTool( |
| 80 | + mcp.NewTool( |
| 81 | + "compile_proto", |
| 82 | + mcp.WithDescription("Compile the proto file using the gRPC Federation option"), |
| 83 | + mcp.WithString( |
| 84 | + "path", |
| 85 | + mcp.Description("The absolute path to the proto file to be analyzed"), |
| 86 | + mcp.Required(), |
| 87 | + ), |
| 88 | + mcp.WithArray( |
| 89 | + "import_paths", |
| 90 | + mcp.Description("Specify the list of import paths required to locate dependency files during compilation. It is recommended to obtain this list using the get_import_proto_list tool"), |
| 91 | + mcp.Required(), |
| 92 | + ), |
| 93 | + ), |
| 94 | + func(ctx context.Context, r mcp.CallToolRequest) (*mcp.CallToolResult, error) { |
| 95 | + args, ok := r.Params.Arguments.(map[string]any) |
| 96 | + if !ok { |
| 97 | + return nil, fmt.Errorf("unexpected argument format: %T", r.Params.Arguments) |
| 98 | + } |
| 99 | + path, ok := args["path"].(string) |
| 100 | + if !ok { |
| 101 | + return nil, fmt.Errorf("failed to find path parameter from arguments: %v", args) |
| 102 | + } |
| 103 | + importPathsArg, ok := args["import_paths"].([]any) |
| 104 | + if !ok { |
| 105 | + return nil, fmt.Errorf("failed to find import_paths parameter from arguments: %v", args) |
| 106 | + } |
| 107 | + content, err := os.ReadFile(path) |
| 108 | + if err != nil { |
| 109 | + return nil, fmt.Errorf("failed to read file: %w", err) |
| 110 | + } |
| 111 | + file, err := source.NewFile(path, content) |
| 112 | + if err != nil { |
| 113 | + return nil, err |
| 114 | + } |
| 115 | + importPaths := make([]string, 0, len(importPathsArg)) |
| 116 | + for _, p := range importPathsArg { |
| 117 | + importPath, ok := p.(string) |
| 118 | + if !ok { |
| 119 | + return nil, fmt.Errorf("failed to get import_paths element. required type is string but got %T", p) |
| 120 | + } |
| 121 | + importPaths = append(importPaths, importPath) |
| 122 | + } |
| 123 | + v := validator.New() |
| 124 | + outs := v.Validate(context.Background(), file, validator.ImportPathOption(importPaths...)) |
| 125 | + if validator.ExistsError(outs) { |
| 126 | + return nil, fmt.Errorf("failed to compile:\n%s", validator.Format(outs)) |
| 127 | + } |
| 128 | + return &mcp.CallToolResult{ |
| 129 | + Content: []mcp.Content{ |
| 130 | + mcp.TextContent{ |
| 131 | + Type: "text", |
| 132 | + Text: "build successful", |
| 133 | + }, |
| 134 | + }, |
| 135 | + }, nil |
| 136 | + }, |
| 137 | + ) |
| 138 | + |
| 139 | + s.AddResource( |
| 140 | + mcp.NewResource( |
| 141 | + "grpc-federation", |
| 142 | + "grpc-federation", |
| 143 | + mcp.WithResourceDescription("gRPC Federation Document"), |
| 144 | + mcp.WithMIMEType("text/markdown"), |
| 145 | + ), |
| 146 | + func(ctx context.Context, request mcp.ReadResourceRequest) ([]mcp.ResourceContents, error) { |
| 147 | + return []mcp.ResourceContents{ |
| 148 | + &mcp.TextResourceContents{ |
| 149 | + URI: "https://github.com/mercari/grpc-federation", |
| 150 | + MIMEType: "text/markdown", |
| 151 | + Text: string(docs), |
| 152 | + }, |
| 153 | + }, nil |
| 154 | + }, |
| 155 | + ) |
| 156 | + |
| 157 | + s.AddPrompt( |
| 158 | + mcp.NewPrompt( |
| 159 | + "grpc_federation", |
| 160 | + mcp.WithPromptDescription("How to use gRPC Federation"), |
| 161 | + ), |
| 162 | + func(ctx context.Context, request mcp.GetPromptRequest) (*mcp.GetPromptResult, error) { |
| 163 | + return mcp.NewGetPromptResult("How to use gRPC Federation", |
| 164 | + []mcp.PromptMessage{ |
| 165 | + mcp.NewPromptMessage( |
| 166 | + mcp.RoleAssistant, |
| 167 | + mcp.NewTextContent(string(docs)), |
| 168 | + ), |
| 169 | + }, |
| 170 | + ), nil |
| 171 | + }, |
| 172 | + ) |
| 173 | + |
| 174 | + // Start the server |
| 175 | + if err := server.ServeStdio(s); err != nil { |
| 176 | + log.Fatalf("failed to start server: %v", err) |
| 177 | + } |
| 178 | +} |
0 commit comments