-
Notifications
You must be signed in to change notification settings - Fork 339
feat: Add custom MCP server support #150
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
derhally
wants to merge
6
commits into
AutoForgeAI:master
Choose a base branch
from
derhally:zd/extra-tools
base: master
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
+1,040
−3
Open
Changes from all commits
Commits
Show all changes
6 commits
Select commit
Hold shift + click to select a range
309de23
feat: Add custom MCP server support
derhally 5e98858
Remove whitespace from blank line
derhally 8284151
Update client.py
derhally e7a37c7
Update client.py
derhally 0cd7177
Remove duplicate lines from bad merge
derhally c8ef489
Adjust comment indentation
derhally File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Some comments aren't visible on the classic Files Changed page.
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
🧩 Analysis chain
🏁 Script executed:
Repository: leonvanzyl/autocoder
Length of output: 88
🏁 Script executed:
Repository: leonvanzyl/autocoder
Length of output: 1468
🌐 Web query:
Claude Agent SDK MCP environment variables merge parent environment💡 Result:
In the Claude Agent SDK, the
envobjects you set:options.env, e.g.ENABLE_TOOL_SEARCH) andoptions.mcpServers.<name>.env, e.g.GITHUB_TOKEN)are intended to be additive overrides on top of the runtime’s existing environment (so you generally don’t need to restate things like
PATH; you just set/override the specific variables you need). The Agent SDK docs show usingenvspecifically to pass credentials and feature flags this way. [1]Notes / gotchas:
envsection were not passed through to MCP servers as expected. [3]Sources:
[1] Anthropic “Connect to external tools with MCP” (Agent SDK) docs https://platform.claude.com/docs/en/agent-sdk/mcp
[2] anthropics/claude-code issue
#5202(env vars like PATH/PYTHONPATH not preserved exactly) anthropics/claude-code#5202[3] anthropics/claude-code issue
#1254(env vars in config not passed to MCP servers) anthropics/claude-code#1254🌐 Web query:
MCP Model Context Protocol environment configuration SDK implementation💡 Result:
MCP environment configuration (what to implement)
1) Configure MCP servers + per-server environment (
.mcp.json)In Claude/Agent SDK flows, MCP servers are commonly declared in a project-root
.mcp.json, and each server can receive anenvobject (passed to the MCP server process). Example shown in the Claude docs:env: { "ALLOWED_PATHS": "..." }. [1]2) Allow shared configs with environment-variable expansion
If you want portable configs (no secrets committed), implement/expect
${VAR}and${VAR:-default}expansion in.mcp.jsonfields includingcommand,args,env,url, andheaders(Claude Code supports this). [2]3) SDK-side “in code” configuration (instead of a file)
Claude’s Agent SDK allows passing MCP server definitions directly via an
options.mcpServersobject when callingquery(). [1]4) Application environment loading (Node/TS example)
A typical Node implementation loads environment variables (e.g., from
.env) before constructing clients/transports (the MCP Node client tutorial usesdotenv.config()and readsANTHROPIC_API_KEYfrom.env). [3]Minimal reference implementations
A)
.mcp.json(stdio server with env){ "mcpServers": { "filesystem": { "command": "npx", "args": ["-y", "@modelcontextprotocol/server-filesystem"], "env": { "ALLOWED_PATHS": "/Users/me/projects" } } } }[1]
B)
.mcp.jsonwith env-var expansion (remote HTTP/SSE auth){ "mcpServers": { "api-server": { "type": "http", "url": "${API_BASE_URL:-https://api.example.com}/mcp", "headers": { "Authorization": "Bearer ${API_KEY}" } } } }[2]
C) Node client setup (dotenv + MCP TS SDK transport)
[3]
SDKs you can base your implementation on
Sources: [1] Claude Agent SDK “MCP in the SDK” docs, [2] Claude Code MCP docs (env expansion), [3] modelcontextprotocol.io Node client tutorial, [4] modelcontextprotocol/swift-sdk README.
🏁 Script executed:
Repository: leonvanzyl/autocoder
Length of output: 3494
🏁 Script executed:
Repository: leonvanzyl/autocoder
Length of output: 2911
🏁 Script executed:
Repository: leonvanzyl/autocoder
Length of output: 4333
Update comment accuracy and apply defensive env merge to handle known SDK issues.
The comment at line 533 claims "merging with parent environment" but the code only performs variable substitution without actually merging. The Claude Agent SDK is documented to treat
envas additive overrides, however there are known issues in the SDK (#5202,#1254) where environment variables from MCP server configs are not correctly passed through. To ensure custom servers receive required variables likePATH, apply the defensive merge:Fix: Explicitly merge env with parent
Also update the comment on line 533 to:
# Add env with variable substitution (defensive merge with parent)🤖 Prompt for AI Agents
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@leonvanzyl would you be ok with this suggest change?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.