MCPcopy Create free account
hub / github.com/coder/coder / ParseConfig

Function ParseConfig

agent/x/agentmcp/config.go:41–91  ·  view source on GitHub ↗

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)

Source from the content-addressed store, hash-verified

39// MCP servers sorted by name. It returns an empty slice when the
40// mcpServers key is missing or empty.
41func 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

Callers 4

TestParseConfigFunction · 0.92
parseAndDedupMethod · 0.85

Calls 7

inferTransportFunction · 0.85
resolveEnvVarsFunction · 0.85
CompareMethod · 0.80
ReadFileMethod · 0.65
ErrorfMethod · 0.45
UnmarshalMethod · 0.45
ContainsMethod · 0.45

Tested by 3

TestParseConfigFunction · 0.74