| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172 |
- package main
- import (
- "encoding/json"
- "fmt"
- "os"
- "path/filepath"
- )
- // MCPServerConfig represents the configuration for a single MCP server
- type MCPServerConfig struct {
- Command string `json:"command"`
- Args []string `json:"args"`
- Env map[string]string `json:"env,omitempty"`
- }
- // MCPConfig represents the root configuration for MCP servers
- type MCPConfig struct {
- MCPServers map[string]MCPServerConfig `json:"mcpServers"`
- }
- // LoadMCPConfig loads the MCP configuration from a JSON file
- func LoadMCPConfig(path string) (*MCPConfig, error) {
- // If no path provided, return empty config
- if path == "" {
- return &MCPConfig{MCPServers: make(map[string]MCPServerConfig)}, nil
- }
- // Check if file exists
- if _, err := os.Stat(path); os.IsNotExist(err) {
- // File doesn't exist, return empty config
- return &MCPConfig{MCPServers: make(map[string]MCPServerConfig)}, nil
- }
- data, err := os.ReadFile(path)
- if err != nil {
- return nil, fmt.Errorf("failed to read MCP config file: %w", err)
- }
- var config MCPConfig
- if err := json.Unmarshal(data, &config); err != nil {
- return nil, fmt.Errorf("failed to parse MCP config file: %w", err)
- }
- if config.MCPServers == nil {
- config.MCPServers = make(map[string]MCPServerConfig)
- }
- return &config, nil
- }
- // DefaultMCPConfigPath returns the default path for mcp.json
- // It looks in the same directory as the .env file
- func DefaultMCPConfigPath() string {
- // Check for mcp.json in current directory first
- if _, err := os.Stat("mcp.json"); err == nil {
- return "mcp.json"
- }
- // Check for mcp.json in the same directory as the executable
- execPath, err := os.Executable()
- if err == nil {
- execDir := filepath.Dir(execPath)
- mcpPath := filepath.Join(execDir, "mcp.json")
- if _, err := os.Stat(mcpPath); err == nil {
- return mcpPath
- }
- }
- // Default to mcp.json in current directory
- return "mcp.json"
- }
|