ParseConfig reads a .mcp.json file at path and returns the declared MCP servers sorted by name. It returns an empty slice when the mcpServers key is missing or empty.
(path string)
| 39 | // MCP servers sorted by name. It returns an empty slice when the |
| 40 | // mcpServers key is missing or empty. |
| 41 | func ParseConfig(path string) ([]ServerConfig, error) { |
| 42 | data, err := os.ReadFile(path) |
| 43 | if err != nil { |
| 44 | return nil, xerrors.Errorf("read mcp config %q: %w", path, err) |
| 45 | } |
| 46 | |
| 47 | var cfg mcpConfigFile |
| 48 | if err := json.Unmarshal(data, &cfg); err != nil { |
| 49 | return nil, xerrors.Errorf("parse mcp config %q: %w", path, err) |
| 50 | } |
| 51 | |
| 52 | if len(cfg.MCPServers) == 0 { |
| 53 | return []ServerConfig{}, nil |
| 54 | } |
| 55 | |
| 56 | servers := make([]ServerConfig, 0, len(cfg.MCPServers)) |
| 57 | for name, raw := range cfg.MCPServers { |
| 58 | var entry mcpServerEntry |
| 59 | if err := json.Unmarshal(raw, &entry); err != nil { |
| 60 | return nil, xerrors.Errorf("parse server %q in %q: %w", name, path, err) |
| 61 | } |
| 62 | |
| 63 | if strings.Contains(name, ToolNameSep) || strings.HasPrefix(name, "_") || strings.HasSuffix(name, "_") { |
| 64 | return nil, xerrors.Errorf("server name %q in %q contains reserved separator %q or leading/trailing underscore", name, path, ToolNameSep) |
| 65 | } |
| 66 | |
| 67 | transport := inferTransport(entry) |
| 68 | |
| 69 | if transport == "" { |
| 70 | return nil, xerrors.Errorf("server %q in %q has no command or url", name, path) |
| 71 | } |
| 72 | |
| 73 | resolveEnvVars(entry.Env) |
| 74 | |
| 75 | servers = append(servers, ServerConfig{ |
| 76 | Name: name, |
| 77 | Transport: transport, |
| 78 | Command: entry.Command, |
| 79 | Args: entry.Args, |
| 80 | Env: entry.Env, |
| 81 | URL: entry.URL, |
| 82 | Headers: entry.Headers, |
| 83 | }) |
| 84 | } |
| 85 | |
| 86 | slices.SortFunc(servers, func(a, b ServerConfig) int { |
| 87 | return strings.Compare(a.Name, b.Name) |
| 88 | }) |
| 89 | |
| 90 | return servers, nil |
| 91 | } |
| 92 | |
| 93 | // inferTransport determines the transport type for a server entry. |
| 94 | // An explicit "type" field takes priority; otherwise the presence |