feat(mcp): add per-entrypoint proxy support for MCP servers#279
feat(mcp): add per-entrypoint proxy support for MCP servers#279nanbingxyz merged 4 commits intonanbingxyz:mainfrom
Conversation
- Add optional `proxy` field to IMCPServer interface - Allow specifying a proxy for each server in mcp.config.ts - Inject proxy as HTTP_PROXY, HTTPS_PROXY, and ALL_PROXY for process-based entrypoints - Prepare activation logic for future HTTP/SSE proxy support (pending SDK capability) - Non-breaking: servers without a proxy field are unaffected Closes nanbingxyz#259
|
✨ No issues found! Your code is sparkling clean! ✨ Need help? Join our Discord for support! |
|
""" WalkthroughProxy configuration support has been introduced for MCP server entrypoints and chat service providers. The configuration schema, type definitions, and sample server config now include an optional Changes
Sequence Diagram(s)sequenceDiagram
participant User
participant MCPClient
participant MCPServer (Local)
participant Proxy
User->>MCPClient: Activate server (with proxy in config)
MCPClient->>MCPServer (Local): Spawn process with proxy env vars (HTTP_PROXY, etc.)
MCPServer (Local)->>Proxy: Outbound requests use proxy settings
Proxy->>MCPServer (Local): Forwards traffic
sequenceDiagram
participant ChatClient
participant ChatService
participant Proxy
participant RemoteAPI
ChatClient->>ChatService: makeRequest with proxy config
ChatService->>Proxy: Create HTTPS proxy agent (if proxy set)
ChatService->>RemoteAPI: fetch request via proxy agent
RemoteAPI-->>ChatService: Response
ChatService-->>ChatClient: Return response
Assessment against linked issues
Poem
Warning There were issues while running some tools. Please review the errors and either fix the tool's configuration or disable the tool if it's a critical failure. 🔧 ESLint
npm error Exit handler never called! 📜 Recent review detailsConfiguration used: CodeRabbit UI ⛔ Files ignored due to path filters (1)
📒 Files selected for processing (7)
🚧 Files skipped from review as they are similar to previous changes (6)
🔇 Additional comments (1)
✨ Finishing Touches
Thanks for using CodeRabbit! It's free for OSS, and your support helps us grow. If you like it, consider giving us a shout-out. 🪧 TipsChatThere are 3 ways to chat with CodeRabbit:
SupportNeed help? Create a ticket on our support page for assistance with any issues or questions. Note: Be mindful of the bot's finite context window. It's strongly recommended to break down tasks such as reading entire modules into smaller chunks. For a focused discussion, use review comments to chat about specific files and their changes, instead of using the PR comments. CodeRabbit Commands (Invoked using PR comments)
Other keywords and placeholders
CodeRabbit Configuration File (
|
There was a problem hiding this comment.
Actionable comments posted: 0
🧹 Nitpick comments (2)
src/mcp.config.ts (1)
12-12: Consider adding a comment to clarify this is an example configuration.The hardcoded proxy URL
http://localhost:8080serves as a good example, but could cause confusion or connection issues if users copy this configuration without understanding it's demonstrative.Consider adding a comment to make this clearer:
isActive: false, + // Example proxy configuration - replace with your actual proxy URL proxy: 'http://localhost:8080',src/main/mcp.ts (1)
274-280: Correct implementation of proxy environment variables.The conditional spread operator properly sets standard proxy environment variables (
HTTP_PROXY,HTTPS_PROXY,ALL_PROXY) when a proxy is configured. This follows Node.js conventions for proxy configuration.Consider adding basic URL validation for the proxy field to catch obvious configuration errors early:
const mergedEnv = { ...getDefaultEnvironment(), ...env, PATH: process.env.PATH, ...(proxy ? { + // Basic validation: ensure proxy URL looks valid + ...((() => { + try { + new URL(proxy); + } catch (error) { + logging.error(`Invalid proxy URL: ${proxy}`, error); + throw new Error(`Invalid proxy URL: ${proxy}`); + } + return {}; + })()), HTTP_PROXY: proxy, HTTPS_PROXY: proxy, ALL_PROXY: proxy, } : {}), };
📜 Review details
Configuration used: CodeRabbit UI
Review profile: CHILL
Plan: Pro
📒 Files selected for processing (3)
src/main/mcp.ts(3 hunks)src/mcp.config.ts(1 hunks)src/types/mcp.d.ts(1 hunks)
🔇 Additional comments (3)
src/types/mcp.d.ts (1)
14-14: LGTM! Clean type definition with helpful documentation.The optional
proxyfield is properly typed and includes a clear comment with an example URL format.src/main/mcp.ts (2)
233-234: Good documentation of future HTTP transport proxy support.The comments clearly indicate that HTTP/SSE proxy support is planned but not yet implemented. This helps set proper expectations.
Also applies to: 239-242
265-265: Clean destructuring to extract proxy configuration.The destructuring correctly extracts the proxy field alongside other server configuration properties.
- Add optional `proxy` field to IMCPServer interface - Allow specifying a proxy for each server in mcp.config.ts - Inject proxy as HTTP_PROXY, HTTPS_PROXY, and ALL_PROXY for process-based entrypoints - Add URL validation to catch invalid proxy configurations early - Prepare activation logic for future HTTP/SSE proxy support (pending SDK capability) - Non-breaking: servers without a proxy field are unaffected Closes nanbingxyz#259
|
Thanks for your contribution! 🙌 Great to see improvements around proxy support. Quick note regarding the proxied models scope: As mentioned in #259, we should also consider proxies for model providers (like OpenAI), not just the MCP server. This helps users in regions where services like api.openai.com might be inaccessible directly. Could we expand this to cover those cases too? Really appreciate you driving this forward! 🚀 |
I have added support for OpenAI models. Do you want to extend this to other providers (Anthropic, Google, etc.)? |
I noticed it adds support for the mcp server proxy, but I don't see specific OpenAI model support yet. Let's focus first on finalizing the core proxy mechanism. My thought is to allow setting a proxy per provider (OpenAI, Anthropic, etc.). We can handle storing the proxy configs and updating the UI separately in a follow-up. Does that approach work for you? |
…ders - Added `proxy` field with example and documentation comment to all provider config files (OpenAI, Anthropic, Google, Azure, Baidu, Moonshot, Mistral, DeepSeek, Ollama, LMStudio, Grok, Doubao, 5ire) - Ensured all provider configs are type-safe and match ProviderType union - Fixed linter/type error for 5ire provider by using correct name This enables users to easily set up per-provider proxying for any supported model provider.
There was a problem hiding this comment.
Actionable comments posted: 4
🧹 Nitpick comments (5)
src/intellichat/services/AnthropicChatService.ts (1)
247-256: Consider improving error logging and validation.The proxy implementation is solid, but consider these improvements:
- Logging:
console.errormay not be suitable for production environments where structured logging is preferred.- Validation: Consider validating the proxy URL format before creating the HttpsProxyAgent.
// Proxy support: if provider.proxy is set, use https-proxy-agent let agent; if (provider.proxy) { + // Basic URL validation + if (!provider.proxy.startsWith('http://') && !provider.proxy.startsWith('https://')) { + throw new Error(`Invalid proxy URL format: ${provider.proxy}`); + } try { agent = new HttpsProxyAgent(provider.proxy); } catch (error) { - console.error(`Invalid proxy URL for provider: ${provider.proxy}`, error); + debug(`Invalid proxy URL for provider: ${provider.proxy}`, error); throw error; } }src/intellichat/services/GoogleChatService.ts (1)
269-278: Consistent implementation with room for improvement.The proxy implementation follows the same pattern as AnthropicChatService, which is good for consistency. However, the same improvement opportunities apply here:
- Logging: Consider using the existing debug logger instead of
console.error.- Validation: Add URL format validation before HttpsProxyAgent creation.
// Proxy support: if provider.proxy is set, use https-proxy-agent let agent; if (provider.proxy) { + // Basic URL validation + if (!provider.proxy.startsWith('http://') && !provider.proxy.startsWith('https://')) { + throw new Error(`Invalid proxy URL format: ${provider.proxy}`); + } try { agent = new HttpsProxyAgent(provider.proxy); } catch (error) { - console.error(`Invalid proxy URL for provider: ${provider.proxy}`, error); + debug(`Invalid proxy URL for provider: ${provider.proxy}`, error); throw error; } }src/providers/Doubao.ts (1)
265-266: Consider using HTTPS for the proxy example.The proxy configuration example uses HTTP instead of HTTPS. For security best practices, consider using HTTPS in the example.
// Example proxy configuration - replace with your actual proxy URL -proxy: 'http://localhost:8080', +proxy: 'https://localhost:8080',src/providers/Azure.ts (1)
267-268: Consider HTTPS for security in proxy example.The proxy example uses HTTP which may not align with security best practices.
// Example proxy configuration - replace with your actual proxy URL -proxy: 'http://localhost:8080', +proxy: 'https://localhost:8080',src/providers/Fire.ts (1)
200-201: Use HTTPS for proxy example security.The proxy example uses HTTP instead of HTTPS, which may not reflect security best practices.
// Example proxy configuration - replace with your actual proxy URL -proxy: 'http://localhost:8080', +proxy: 'https://localhost:8080',
📜 Review details
Configuration used: CodeRabbit UI
Review profile: CHILL
Plan: Pro
📒 Files selected for processing (17)
src/intellichat/services/AnthropicChatService.ts(3 hunks)src/intellichat/services/GoogleChatService.ts(2 hunks)src/intellichat/services/OpenAIChatService.ts(3 hunks)src/providers/Anthropic.ts(1 hunks)src/providers/Azure.ts(2 hunks)src/providers/Baidu.ts(1 hunks)src/providers/DeepSeek.ts(1 hunks)src/providers/Doubao.ts(1 hunks)src/providers/Fire.ts(2 hunks)src/providers/Google.ts(1 hunks)src/providers/Grok.ts(1 hunks)src/providers/LMStudio.ts(1 hunks)src/providers/Mistral.ts(1 hunks)src/providers/Moonshot.ts(1 hunks)src/providers/Ollama.ts(1 hunks)src/providers/OpenAI.ts(2 hunks)src/providers/types.ts(1 hunks)
✅ Files skipped from review due to trivial changes (7)
- src/providers/types.ts
- src/providers/Anthropic.ts
- src/providers/Baidu.ts
- src/providers/Google.ts
- src/providers/Moonshot.ts
- src/providers/DeepSeek.ts
- src/providers/Grok.ts
🧰 Additional context used
🧬 Code Graph Analysis (1)
src/intellichat/services/OpenAIChatService.ts (1)
src/main/logging.ts (1)
error(38-40)
🔇 Additional comments (16)
src/providers/Ollama.ts (1)
6-7: LGTM: Proxy configuration follows established pattern.The proxy configuration is consistent with other provider files and includes helpful documentation.
src/providers/OpenAI.ts (2)
194-194: LGTM: Quote consistency improvement.Good fix replacing curly quotes with standard ASCII quotes for better consistency.
261-262: LGTM: Proxy configuration follows established pattern.The proxy configuration is consistent with other provider files and includes helpful documentation.
src/providers/Mistral.ts (2)
105-105: LGTM: Apostrophe consistency improvement.Good fix replacing the typographic apostrophe with a standard ASCII apostrophe for better consistency.
112-113: LGTM: Proxy configuration follows established pattern.The proxy configuration is consistent with other provider files and includes helpful documentation.
src/providers/LMStudio.ts (1)
6-7: LGTM: Proxy configuration follows established pattern.The proxy configuration is consistent with other provider files and includes helpful documentation.
src/intellichat/services/OpenAIChatService.ts (2)
21-21: LGTM: Proper import for proxy support.Correct import of HttpsProxyAgent for proxy functionality.
286-286: LGTM: Correct conditional agent inclusion.The conditional spread operator correctly includes the agent only when it's defined.
src/intellichat/services/AnthropicChatService.ts (2)
27-27: LGTM: Proxy agent import added.The import of
HttpsProxyAgentis correctly placed and aligns with the proxy support implementation.
266-266: LGTM: Conditional agent application.The conditional spread operator correctly applies the agent only when it exists, maintaining backward compatibility.
src/intellichat/services/GoogleChatService.ts (2)
28-28: LGTM: Consistent proxy agent import.The import matches the pattern established in AnthropicChatService, maintaining consistency across chat services.
286-286: LGTM: Consistent conditional agent application.The conditional spread operator usage matches other services, ensuring consistency across the codebase.
src/providers/Doubao.ts (1)
264-264: Confirm Doubao API endpointThe HEAD request to
https://api.doubao.com/v1did not return a response. Please verify that this new base URL is correct and accessible—e.g., try a GET against the root or consult the API docs.• File: src/providers/Doubao.ts
• Line: 264Also consider whether the proxy example should use
https://instead ofhttp://for consistency and security.src/providers/Azure.ts (1)
194-194: LGTM: Typography correction.Good fix replacing curly quotes with straight quotes for better text consistency.
src/providers/Fire.ts (2)
158-158: LGTM: Typography correction.Good consistency improvement replacing curly quotes with straight quotes.
199-199: Manual verification required: New API domain unreachableThe sandbox environment failed to resolve api.5ire.com, so we can’t confirm availability of the new endpoint. Please verify:
- That https://api.5ire.com/v1 is the correct, live API base URL.
- DNS records for api.5ire.com are published and propagating.
- The service is up and responding (e.g., via
dig,nslookup, or directcurl -Ifrom a network with internet access).
…esolve process type error - Dynamically set apiBase in Azure provider using AZURE_RESOURCE_NAME and AZURE_DEPLOYMENT_ID environment variables - Added instructions to install @types/node to resolve process type error - Updated proxy fallback logic and placeholder consistency in Ollama and LMStudio providers - Improved proxy error handling in OpenAI, Anthropic, and Google chat services
|
Cool! |
Summary
This PR implements per-entrypoint proxy support for MCP servers, addressing #259. Users can now specify a proxy for each entrypoint/server individually, enabling mixed proxy and direct connections within the same app instance.
What’s Changed
Type Update:
proxyfield to theIMCPServerinterface (src/types/mcp.d.ts).Config Update:
proxyfield insrc/mcp.config.ts.Activation Logic:
proxyis set, the environment will includeHTTP_PROXY,HTTPS_PROXY, andALL_PROXY.How it Works
Process-based entrypoints:
If a server config includes a
proxyfield, the proxy is injected as environment variables when the process is started.No breaking changes for users who do not specify a proxy.
HTTP/SSE entrypoints:
The code is ready to pass a proxy option to the transport if/when the SDK supports it.
Currently, this is a no-op, but the code is structured for easy future extension.
Why
Next Steps / Notes
Closes #259
Summary by CodeRabbit
New Features
Chores