Skip to content

Commit 88a8c7b

Browse files
authored
support anthropicBaseURL , openaiAPIKey, anthropicAPIKey cli params (#6)
* * support anthropicBaseURL , openaiAPIKey, anthropicAPIKey cli params * * support anthropicBaseURL , openaiAPIKey, anthropicAPIKey cli params * opt * remove the non-English comments * add new Flags to document
1 parent b32fd14 commit 88a8c7b

File tree

6 files changed

+60
-22
lines changed

6 files changed

+60
-22
lines changed

.gitignore

+5
Original file line numberDiff line numberDiff line change
@@ -3,3 +3,8 @@
33
aidocs/
44
.mcp.json
55
*.log
6+
mcphost
7+
.idea
8+
test/
9+
build/
10+
scripts/

README.md

+5-1
Original file line numberDiff line numberDiff line change
@@ -112,11 +112,15 @@ mcphost -m openai:gpt-4
112112
```
113113

114114
### Flags
115+
- `--anthropic-url string`: Base URL for Anthropic API (defaults to api.anthropic.com)
116+
- `--anthropic-api-key string`: Anthropic API key (can also be set via ANTHROPIC_API_KEY environment variable)
115117
- `--config string`: Config file location (default is $HOME/mcp.json)
116118
- `--debug`: Enable debug logging
117119
- `--message-window int`: Number of messages to keep in context (default: 10)
118120
- `-m, --model string`: Model to use (format: provider:model) (default "anthropic:claude-3-5-sonnet-latest")
119121
- `--openai-url string`: Base URL for OpenAI API (defaults to api.openai.com)
122+
- `--openai-api-key string`: OpenAI API key (can also be set via OPENAI_API_KEY environment variable)
123+
120124

121125
### Interactive Commands
122126

@@ -154,4 +158,4 @@ This project is licensed under the MIT License - see the [LICENSE](LICENSE) file
154158

155159
- Thanks to the Anthropic team for Claude and the MCP specification
156160
- Thanks to the Ollama team for their local LLM runtime
157-
- Thanks to all contributors who have helped improve this tool
161+
- Thanks to all contributors who have helped improve this tool

cmd/root.go

+27-12
Original file line numberDiff line numberDiff line change
@@ -26,11 +26,14 @@ import (
2626
)
2727

2828
var (
29-
renderer *glamour.TermRenderer
30-
configFile string
31-
messageWindow int
32-
modelFlag string // New flag for model selection
33-
openaiBaseURL string // Base URL for OpenAI API
29+
renderer *glamour.TermRenderer
30+
configFile string
31+
messageWindow int
32+
modelFlag string // New flag for model selection
33+
openaiBaseURL string // Base URL for OpenAI API
34+
anthropicBaseURL string // Base URL for Anthropic API
35+
openaiAPIKey string
36+
anthropicAPIKey string
3437
)
3538

3639
const (
@@ -79,8 +82,12 @@ func init() {
7982
// Add debug flag
8083
rootCmd.PersistentFlags().
8184
BoolVar(&debugMode, "debug", false, "enable debug logging")
82-
rootCmd.PersistentFlags().
83-
StringVar(&openaiBaseURL, "openai-url", "", "base URL for OpenAI API (defaults to api.openai.com)")
85+
86+
flags := rootCmd.PersistentFlags()
87+
flags.StringVar(&openaiBaseURL, "openai-url", "", "base URL for OpenAI API (defaults to api.openai.com)")
88+
flags.StringVar(&anthropicBaseURL, "anthropic-url", "", "base URL for Anthropic API (defaults to api.anthropic.com)")
89+
flags.StringVar(&openaiAPIKey, "openai-api-key", "", "OpenAI API key")
90+
flags.StringVar(&anthropicAPIKey, "anthropic-api-key", "", "Anthropic API key")
8491
}
8592

8693
// Add new function to create provider
@@ -98,22 +105,30 @@ func createProvider(modelString string) (llm.Provider, error) {
98105

99106
switch provider {
100107
case "anthropic":
101-
apiKey := os.Getenv("ANTHROPIC_API_KEY")
108+
apiKey := anthropicAPIKey
109+
if apiKey == "" {
110+
apiKey = os.Getenv("ANTHROPIC_API_KEY")
111+
}
112+
102113
if apiKey == "" {
103114
return nil, fmt.Errorf(
104-
"ANTHROPIC_API_KEY environment variable not set",
115+
"Anthropic API key not provided. Use --anthropic-api-key flag or ANTHROPIC_API_KEY environment variable",
105116
)
106117
}
107-
return anthropic.NewProvider(apiKey), nil
118+
return anthropic.NewProvider(apiKey, anthropicBaseURL, model), nil
108119

109120
case "ollama":
110121
return ollama.NewProvider(model)
111122

112123
case "openai":
113-
apiKey := os.Getenv("OPENAI_API_KEY")
124+
apiKey := openaiAPIKey
125+
if apiKey == "" {
126+
apiKey = os.Getenv("OPENAI_API_KEY")
127+
}
128+
114129
if apiKey == "" {
115130
return nil, fmt.Errorf(
116-
"OPENAI_API_KEY environment variable not set",
131+
"OpenAI API key not provided. Use --openai-api-key flag or OPENAI_API_KEY environment variable",
117132
)
118133
}
119134
return openai.NewProvider(apiKey, openaiBaseURL, model), nil

pkg/llm/anthropic/client.go

+14-6
Original file line numberDiff line numberDiff line change
@@ -6,17 +6,25 @@ import (
66
"encoding/json"
77
"fmt"
88
"net/http"
9+
"strings"
910
)
1011

1112
type Client struct {
12-
apiKey string
13-
client *http.Client
13+
apiKey string
14+
client *http.Client
15+
baseURL string
1416
}
1517

16-
func NewClient(apiKey string) *Client {
18+
func NewClient(apiKey string, baseURL string) *Client {
19+
if baseURL == "" {
20+
baseURL = "https://api.anthropic.com/v1"
21+
} else if !strings.HasSuffix(baseURL, "/v1") {
22+
baseURL = strings.TrimSuffix(baseURL, "/") + "/v1"
23+
}
1724
return &Client{
18-
apiKey: apiKey,
19-
client: &http.Client{},
25+
apiKey: apiKey,
26+
baseURL: baseURL,
27+
client: &http.Client{},
2028
}
2129
}
2230

@@ -26,7 +34,7 @@ func (c *Client) CreateMessage(ctx context.Context, req CreateRequest) (*APIMess
2634
return nil, fmt.Errorf("error marshaling request: %w", err)
2735
}
2836

29-
httpReq, err := http.NewRequestWithContext(ctx, "POST", "https://api.anthropic.com/v1/messages", bytes.NewReader(body))
37+
httpReq, err := http.NewRequestWithContext(ctx, "POST", fmt.Sprintf("%s/messages", c.baseURL), bytes.NewReader(body))
3038
if err != nil {
3139
return nil, fmt.Errorf("error creating request: %w", err)
3240
}

pkg/llm/anthropic/provider.go

+6-3
Original file line numberDiff line numberDiff line change
@@ -16,10 +16,13 @@ type Provider struct {
1616
model string
1717
}
1818

19-
func NewProvider(apiKey string) *Provider {
19+
func NewProvider(apiKey string, baseURL string, model string) *Provider {
20+
if model == "" {
21+
model = "claude-3-5-sonnet-20240620" // 默认模型
22+
}
2023
return &Provider{
21-
client: NewClient(apiKey),
22-
model: "claude-3-5-sonnet-20240620",
24+
client: NewClient(apiKey, baseURL),
25+
model: model,
2326
}
2427
}
2528

pkg/llm/openai/client.go

+3
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ import (
66
"encoding/json"
77
"fmt"
88
"net/http"
9+
"strings"
910
)
1011

1112
type Client struct {
@@ -17,6 +18,8 @@ type Client struct {
1718
func NewClient(apiKey string, baseURL string) *Client {
1819
if baseURL == "" {
1920
baseURL = "https://api.openai.com/v1"
21+
} else if !strings.HasSuffix(baseURL, "/v1") {
22+
baseURL = strings.TrimSuffix(baseURL, "/") + "/v1"
2023
}
2124
return &Client{
2225
apiKey: apiKey,

0 commit comments

Comments
 (0)