|
| 1 | +/* |
| 2 | +Copyright 2025 API Testing Authors. |
| 3 | +
|
| 4 | +Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | +you may not use this file except in compliance with the License. |
| 6 | +You may obtain a copy of the License at |
| 7 | +
|
| 8 | + http://www.apache.org/licenses/LICENSE-2.0 |
| 9 | +
|
| 10 | +Unless required by applicable law or agreed to in writing, software |
| 11 | +distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | +WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | +See the License for the specific language governing permissions and |
| 14 | +limitations under the License. |
| 15 | +*/ |
| 16 | +package cmd |
| 17 | + |
| 18 | +import ( |
| 19 | + "fmt" |
| 20 | + "net/http" |
| 21 | + "os" |
| 22 | + |
| 23 | + "github.com/linuxsuren/api-testing/pkg/testing" |
| 24 | + "github.com/linuxsuren/atest-ext-store-orm/pkg" |
| 25 | + "github.com/modelcontextprotocol/go-sdk/mcp" |
| 26 | + "github.com/spf13/cobra" |
| 27 | +) |
| 28 | + |
| 29 | +func newMCPCommand() (c *cobra.Command) { |
| 30 | + opt := &mcpOption{} |
| 31 | + c = &cobra.Command{ |
| 32 | + Use: "mcp", |
| 33 | + Short: "Multi-Cluster-Platform related commands", |
| 34 | + PreRunE: opt.preRunE, |
| 35 | + RunE: opt.runE, |
| 36 | + } |
| 37 | + flags := c.Flags() |
| 38 | + flags.StringVarP(&opt.mode, "mode", "", "http", "Server mode, one of http/stdio/sse") |
| 39 | + flags.IntVarP(&opt.port, "port", "", 7072, "Server port for http or sse mode") |
| 40 | + flags.StringVarP(&opt.url, "url", "", "", "Database URL") |
| 41 | + flags.StringVarP(&opt.username, "username", "", "", "Database username") |
| 42 | + flags.StringVarP(&opt.password, "password", "", "", "Database password") |
| 43 | + flags.StringVarP(&opt.database, "database", "", "", "Database name") |
| 44 | + flags.StringVarP(&opt.driver, "driver", "", "mysql", "Database driver, one of mysql/postgres/sqlite") |
| 45 | + return |
| 46 | +} |
| 47 | + |
| 48 | +type mcpOption struct { |
| 49 | + mode string |
| 50 | + port int |
| 51 | + url string |
| 52 | + username string |
| 53 | + password string |
| 54 | + database string |
| 55 | + driver string |
| 56 | +} |
| 57 | + |
| 58 | +func (o *mcpOption) preRunE(c *cobra.Command, args []string) (err error) { |
| 59 | + if o.url = getValueOrEnv(o.url, "DB_URL"); o.url == "" { |
| 60 | + err = fmt.Errorf("database url is required") |
| 61 | + return |
| 62 | + } |
| 63 | + o.username = getValueOrEnv(o.username, "DB_USERNAME") |
| 64 | + o.password = getValueOrEnv(o.password, "DB_PASSWORD") |
| 65 | + o.database = getValueOrEnv(o.database, "DB_DATABASE") |
| 66 | + o.driver = getValueOrEnv(o.driver, "DB_DRIVER") |
| 67 | + return |
| 68 | +} |
| 69 | + |
| 70 | +func getValueOrEnv(value, envKey string) (result string) { |
| 71 | + if value != "" { |
| 72 | + result = value |
| 73 | + } else { |
| 74 | + result = os.Getenv(envKey) |
| 75 | + } |
| 76 | + return |
| 77 | +} |
| 78 | + |
| 79 | +func (o *mcpOption) runE(c *cobra.Command, args []string) (err error) { |
| 80 | + opts := &mcp.ServerOptions{ |
| 81 | + Instructions: "Database query mcp server", |
| 82 | + } |
| 83 | + |
| 84 | + server := mcp.NewServer(&mcp.Implementation{ |
| 85 | + Name: "database-mcp-server", |
| 86 | + Title: "ORM Database MCP Server", |
| 87 | + }, opts) |
| 88 | + |
| 89 | + store := &testing.Store{ |
| 90 | + URL: o.url, |
| 91 | + Username: o.username, |
| 92 | + Password: o.password, |
| 93 | + Properties: map[string]string{ |
| 94 | + "database": o.database, |
| 95 | + "driver": o.driver, |
| 96 | + }, |
| 97 | + } |
| 98 | + |
| 99 | + dbServer := pkg.NewMcpServer(store) |
| 100 | + mcp.AddTool(server, &mcp.Tool{ |
| 101 | + Name: "database-query", |
| 102 | + Description: "Query the database by SQL", |
| 103 | + }, dbServer.Query) |
| 104 | + |
| 105 | + switch o.mode { |
| 106 | + case "sse": |
| 107 | + handler := mcp.NewSSEHandler(func(request *http.Request) *mcp.Server { |
| 108 | + return server |
| 109 | + }) |
| 110 | + c.Println("Starting SSE server on port:", o.port) |
| 111 | + err = http.ListenAndServe(fmt.Sprintf(":%d", o.port), handler) |
| 112 | + case "stdio": |
| 113 | + err = server.Run(c.Context(), &mcp.StdioTransport{}) |
| 114 | + case "http": |
| 115 | + fallthrough |
| 116 | + default: |
| 117 | + handler := mcp.NewStreamableHTTPHandler(func(request *http.Request) *mcp.Server { |
| 118 | + return server |
| 119 | + }, nil) |
| 120 | + c.Println("Starting HTTP server on port:", o.port) |
| 121 | + err = http.ListenAndServe(fmt.Sprintf(":%d", o.port), handler) |
| 122 | + } |
| 123 | + return |
| 124 | +} |
0 commit comments