diff --git a/.env.example b/.env.example index 45284a3c..3cb34c5a 100644 --- a/.env.example +++ b/.env.example @@ -2,6 +2,9 @@ ANTHROPIC_API_KEY=your_anthropic_api_key_here # Format: sk-ant-api03-... PERPLEXITY_API_KEY=your_perplexity_api_key_here # Format: pplx-... +# API Base URLs (Optional) +ANTHROPIC_API_BASE_URL=optional_base_url_here # Optional custom base URL for Anthropic API + # Model Configuration MODEL=claude-3-7-sonnet-20250219 # Recommended models: claude-3-7-sonnet-20250219, claude-3-opus-20240229 PERPLEXITY_MODEL=sonar-pro # Perplexity model for research-backed subtasks diff --git a/context/mcp-protocol-repo.txt b/context/mcp-protocol-repo.txt index a03dc812..ce3c7c05 100644 --- a/context/mcp-protocol-repo.txt +++ b/context/mcp-protocol-repo.txt @@ -3784,6 +3784,7 @@ In this tutorial, you'll learn how to build a LLM-powered chatbot client that co if (!ANTHROPIC_API_KEY) { throw new Error("ANTHROPIC_API_KEY is not set"); } + const ANTHROPIC_API_BASE_URL = process.env.ANTHROPIC_API_BASE_URL; class MCPClient { private mcp: Client; @@ -3794,6 +3795,7 @@ In this tutorial, you'll learn how to build a LLM-powered chatbot client that co constructor() { this.anthropic = new Anthropic({ apiKey: ANTHROPIC_API_KEY, + baseUrl: ANTHROPIC_API_BASE_URL, }); this.mcp = new Client({ name: "mcp-client-cli", version: "1.0.0" }); } diff --git a/mcp-server/src/core/utils/ai-client-utils.js b/mcp-server/src/core/utils/ai-client-utils.js index 57250d09..d1b9fe32 100644 --- a/mcp-server/src/core/utils/ai-client-utils.js +++ b/mcp-server/src/core/utils/ai-client-utils.js @@ -35,9 +35,13 @@ export function getAnthropicClientForMCP(session, log = console) { ); } + const baseURL = + session?.env?.ANTHROPIC_API_BASE_URL || process.env.ANTHROPIC_API_BASE_URL; + // Initialize and return a new Anthropic client return new Anthropic({ apiKey, + baseURL: baseURL, // Will be undefined if not set, defaulting to Anthropic's standard URL defaultHeaders: { 'anthropic-beta': 'output-128k-2025-02-19' // Include header for increased token limit } diff --git a/scripts/modules/ai-services.js b/scripts/modules/ai-services.js index 1f37fbcb..31918a59 100644 --- a/scripts/modules/ai-services.js +++ b/scripts/modules/ai-services.js @@ -18,6 +18,7 @@ dotenv.config(); // Configure Anthropic client const anthropic = new Anthropic({ apiKey: process.env.ANTHROPIC_API_KEY, + baseURL: process.env.ANTHROPIC_API_BASE_URL, // Add beta header for 128k token output defaultHeaders: { 'anthropic-beta': 'output-128k-2025-02-19' @@ -1297,8 +1298,11 @@ function getAnthropicClient(session) { ); } + const baseUrl = session?.env?.ANTHROPIC_API_BASE_URL || process.env.ANTHROPIC_API_BASE_URL; + return new Anthropic({ apiKey: apiKey, + baseURL: baseUrl, // Add beta header for 128k token output defaultHeaders: { 'anthropic-beta': 'output-128k-2025-02-19' @@ -1465,6 +1469,11 @@ function getConfiguredAnthropicClient(session = null, customEnv = null) { process.env.ANTHROPIC_API_KEY || customEnv?.ANTHROPIC_API_KEY; + const baseUrl = + session?.ANTHROPIC_API_BASE_URL || + process?.env?.ANTHROPIC_API_BASE_URL || + customEnv.env.ANTHROPIC_API_BASE_URL; + if (!apiKey) { throw new Error( 'ANTHROPIC_API_KEY environment variable is missing. Set it to use AI features.' @@ -1473,6 +1482,7 @@ function getConfiguredAnthropicClient(session = null, customEnv = null) { return new Anthropic({ apiKey: apiKey, + baseURL: baseUrl, // Add beta header for 128k token output defaultHeaders: { 'anthropic-beta': 'output-128k-2025-02-19' diff --git a/scripts/modules/dependency-manager.js b/scripts/modules/dependency-manager.js index af8904fb..b938ab6e 100644 --- a/scripts/modules/dependency-manager.js +++ b/scripts/modules/dependency-manager.js @@ -24,7 +24,8 @@ import { generateTaskFiles } from './task-manager.js'; // Initialize Anthropic client const anthropic = new Anthropic({ - apiKey: process.env.ANTHROPIC_API_KEY + apiKey: process.env.ANTHROPIC_API_KEY, + baseURL: process.env.ANTHROPIC_API_BASE_URL }); /** diff --git a/scripts/modules/task-manager.js b/scripts/modules/task-manager.js index 2e291ec9..a8d31cb6 100644 --- a/scripts/modules/task-manager.js +++ b/scripts/modules/task-manager.js @@ -60,7 +60,8 @@ import { // Initialize Anthropic client const anthropic = new Anthropic({ - apiKey: process.env.ANTHROPIC_API_KEY + apiKey: process.env.ANTHROPIC_API_KEY, + baseURL: process.env.ANTHROPIC_API_BASE_URL }); // Import perplexity if available