Skip to content

Commit 7d952be

Browse files
Cobdogclaude
andauthored
feat: support ANTHROPIC_BASE_URL for third-party endpoints
* feat: support ANTHROPIC_BASE_URL for third-party Anthropic-compatible endpoints Allow users to configure a custom base URL for the Anthropic API client via the ANTHROPIC_BASE_URL environment variable. This enables use with third-party providers (e.g. z.ai) that expose an Anthropic-compatible API without modifying the source. When ANTHROPIC_BASE_URL is not set, behavior is unchanged (default Anthropic endpoint). * fix: align README environment variable table columns Co-Authored-By: Claude Opus 4.6 (1M context) <noreply@anthropic.com> --------- Co-authored-by: Claude Opus 4.6 (1M context) <noreply@anthropic.com>
1 parent 0f7452a commit 7d952be

3 files changed

Lines changed: 52 additions & 6 deletions

File tree

README.md

Lines changed: 7 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -213,11 +213,12 @@ After saving the config, **restart Claude Desktop / Claude Code** for the server
213213

214214
Environment variables are optional:
215215

216-
| Variable | Purpose |
217-
| ------------------- | -------------------------------------------- |
218-
| `GITHUB_TOKEN` | Higher GitHub API limits / private access |
219-
| `ANTHROPIC_API_KEY` | AI-generated summaries via Claude Haiku (takes priority) |
220-
| `GOOGLE_API_KEY` | AI-generated summaries via Gemini Flash |
216+
| Variable | Purpose |
217+
| -------------------- | -------------------------------------------------------- |
218+
| `GITHUB_TOKEN` | Higher GitHub API limits / private access |
219+
| `ANTHROPIC_API_KEY` | AI-generated summaries via Claude Haiku (takes priority) |
220+
| `ANTHROPIC_BASE_URL` | Third-party Anthropic-compatible endpoints (e.g. z.ai) |
221+
| `GOOGLE_API_KEY` | AI-generated summaries via Gemini Flash |
221222

222223
---
223224

@@ -362,6 +363,7 @@ For **LM Studio**, ensure the Local Server is running (usually on port 1234):
362363
| --------------------------- | ------------------------- | -------- |
363364
| `GITHUB_TOKEN` | GitHub API auth | No |
364365
| `ANTHROPIC_API_KEY` | Symbol summaries via Claude Haiku (takes priority) | No |
366+
| `ANTHROPIC_BASE_URL` | Third-party Anthropic-compatible endpoints (e.g. z.ai) | No |
365367
| `GOOGLE_API_KEY` | Symbol summaries via Gemini Flash | No |
366368
| `OPENAI_API_BASE` | Base URL for local LLMs (e.g. `http://localhost:11434/v1`) | No |
367369
| `OPENAI_API_KEY` | API key for local LLMs (default: `local-llm`) | No |

src/jcodemunch_mcp/summarizer/batch_summarize.py

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -63,7 +63,11 @@ def _init_client(self):
6363
from anthropic import Anthropic
6464
api_key = os.environ.get("ANTHROPIC_API_KEY")
6565
if api_key:
66-
self.client = Anthropic(api_key=api_key)
66+
base_url = os.environ.get("ANTHROPIC_BASE_URL")
67+
kwargs = {"api_key": api_key}
68+
if base_url:
69+
kwargs["base_url"] = base_url
70+
self.client = Anthropic(**kwargs)
6771
except ImportError:
6872
if os.environ.get("ANTHROPIC_API_KEY"):
6973
import warnings

tests/test_summarizer.py

Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -110,6 +110,46 @@ def test_simple_summarize_fallback_to_signature():
110110
assert "def foo" in result[0].summary
111111

112112

113+
def test_anthropic_summarizer_base_url():
114+
"""BatchSummarizer passes ANTHROPIC_BASE_URL to Anthropic client when set."""
115+
import sys
116+
117+
mock_anthropic_module = MagicMock()
118+
mock_client = MagicMock()
119+
mock_anthropic_module.Anthropic.return_value = mock_client
120+
121+
with patch.dict(sys.modules, {"anthropic": mock_anthropic_module}):
122+
with patch.dict("os.environ", {
123+
"ANTHROPIC_API_KEY": "sk-test-key",
124+
"ANTHROPIC_BASE_URL": "https://proxy.example.com/v1",
125+
}, clear=True):
126+
from jcodemunch_mcp.summarizer.batch_summarize import BatchSummarizer
127+
summarizer = BatchSummarizer()
128+
129+
mock_anthropic_module.Anthropic.assert_called_once_with(
130+
api_key="sk-test-key",
131+
base_url="https://proxy.example.com/v1",
132+
)
133+
assert summarizer.client is mock_client
134+
135+
136+
def test_anthropic_summarizer_no_base_url():
137+
"""BatchSummarizer omits base_url when ANTHROPIC_BASE_URL is not set."""
138+
import sys
139+
140+
mock_anthropic_module = MagicMock()
141+
mock_client = MagicMock()
142+
mock_anthropic_module.Anthropic.return_value = mock_client
143+
144+
with patch.dict(sys.modules, {"anthropic": mock_anthropic_module}):
145+
with patch.dict("os.environ", {"ANTHROPIC_API_KEY": "sk-test-key"}, clear=True):
146+
from jcodemunch_mcp.summarizer.batch_summarize import BatchSummarizer
147+
summarizer = BatchSummarizer()
148+
149+
mock_anthropic_module.Anthropic.assert_called_once_with(api_key="sk-test-key")
150+
assert summarizer.client is mock_client
151+
152+
113153
def test_gemini_summarizer_no_api_key():
114154
"""GeminiBatchSummarizer falls back to signature when no API key is set."""
115155
with patch.dict("os.environ", {}, clear=True):

0 commit comments

Comments
 (0)