Imported from xihuai18/claude-code-mcp (
AGENTS.md). Install upstream withnpx skills add xihuai18/claude-code-mcp. Copyright stays with the author.
Repo Agent Instructions (claude-code-mcp)
This repository is a TypeScript (ESM) MCP server wrapping Claude Agent SDK / Claude Code CLI.
Package: @leo000001/claude-code-mcp.
Assumption: MCP server and client run on the same machine (same platform), via stdio.
Last Updated: 2026-02-27
Document Boundary (Must Read)
This repo intentionally separates execution rules from design details:
| Topic | Primary Doc |
|---|---|
| How to execute work, upgrade flow, required checks | AGENTS.md |
| Architecture internals, field/message mapping, lifecycle details | docs/DESIGN.md |
| End-user usage guide | README.md |
| Release history | CHANGELOG.md |
Non-negotiable dedupe rule:
- Keep
AGENTS.mdaction-oriented. - Keep long parameter tables, message mapping, and protocol deep dives in
docs/DESIGN.md. - If details are needed, link to DESIGN anchors instead of duplicating.
Visibility rule for MCP-facing guidance:
- Do not assume MCP-connected coding agents can read
README.md,AGENTS.md, ordocs/DESIGN.md. - Put protocol-critical runtime guidance first in MCP tool descriptions and MCP resources.
- Treat repo docs as human/maintainer guidance unless a client explicitly injects them into model context.
Design Snapshot (Summary Only)
Project direction:
- Use local Claude settings by default (
settingSources: ["user","project","local"]) - Expose only 4 MCP tools (
claude_code,claude_code_reply,claude_code_session,claude_code_check) - Keep startup non-blocking (start/reply return quickly; poll with check)
- Provide three-layer permission control (
advanced.tools+ allow/deny + async decision)
Detailed behavior, full field semantics, and lifecycle mapping:
docs/DESIGN.md#sdk-interface-baselinedocs/DESIGN.md#upgrade-methodologydocs/DESIGN.mdSection 4 (Options mapping matrix)docs/DESIGN.mdSection 5 (SDK message mapping matrix)
SDK Upgrade Runbook (Execution Playbook)
When dependency interfaces change (@anthropic-ai/claude-agent-sdk, @modelcontextprotocol/sdk, sometimes zod schema impacts), execute in this order:
- Confirm authoritative type definitions:
node_modules/@anthropic-ai/claude-agent-sdk/sdk.d.ts- relevant MCP SDK type surface in
node_modules/@modelcontextprotocol/sdk
- Compare against design matrices in
docs/DESIGN.md(Sections 4 and 5). - Apply code updates to required touch points:
src/server.ts(zod schema and tool contract)src/utils/build-options.ts(Option field mapping/defaults)src/tools/query-consumer.ts(message mapping/permission callback behavior)src/session/manager.ts(session and permission lifecycle)src/types.ts(shared types/const tuples)
- Sync docs and release notes:
README.mddocs/DESIGN.mdAGENTS.mdCHANGELOG.md
- Run full checks:
npm run typechecknpm run lintnpm testnpm run format:check
Full Maintenance & Dependency Update Workflow
This section is the authoritative end-to-end workflow for updating dependencies and keeping code + docs + tests aligned.
0) Goals and Constraints
- Goal: keep MCP tool contracts stable while staying current with the upstream SDKs.
- Rule: SDK type definitions are authoritative (
sdk.d.ts), changelogs are hints. - Rule: avoid duplicating long tables here; update detailed matrices only in
docs/DESIGN.md. - Rule: do not ship stale lockfiles when
package.jsonuses^ranges (users will resolve newer versions). - Hygiene: any temporary audit artifacts must be deleted before finishing.
1) Detect Updates (Local, Reproducible)
Run:
npm outdated(top-level)npm outdated --all(transitive signal only; don't chase majors unless needed)
Record:
- current / wanted / latest for
@anthropic-ai/claude-agent-sdkand@modelcontextprotocol/sdk - whether
zodstays compatible (SDK peerszod@^4)
2) Establish the Interface Baseline (Authoritative)
Always treat the installed type surface as the source of truth:
- Claude Agent SDK:
node_modules/@anthropic-ai/claude-agent-sdk/sdk.d.ts - MCP SDK:
node_modules/@modelcontextprotocol/sdk/dist/**(only the surfaces we import)
If you need to compare two versions without changing the workspace yet:
- Create a temporary directory under
tmp/(example:tmp/deps-audit/). - Download tarballs via
npm pack(example):npm pack @anthropic-ai/claude-agent-sdk@<old>npm pack @anthropic-ai/claude-agent-sdk@<new>
- Extract and diff
package/sdk.d.ts. - Delete
tmp/deps-audit/after the report is done.
3) Impact Analysis Checklist (What Can Break)
For each upgraded runtime dependency:
- Options surface: compare SDK
Optionsfields tosrc/utils/build-options.ts(OptionSource+ copy logic). - Message surface: compare SDK
SDKMessageunion (newtype/subtype) tosrc/tools/query-consumer.tsmapping. - Tool discovery: if
system/init.toolsadds new tool names, decide whether to add descriptions tosrc/tools/tool-discovery.ts. - Policy filters: if new progress events appear, confirm
claude_code_checkfiltering rules insrc/tools/claude-code-check.ts. - Docs: update
docs/DESIGN.mdmatrices (Section 4 and 5) and ensureREADME.mdmatches behavior.
4) Apply Updates (Code + Docs + Tests)
4.1 Update dependency ranges
- Edit
package.jsonversions.
4.2 Refresh the lockfile
- Run
npm installto updatepackage-lock.json.
4.3 Close the code loop
- If SDK adds a new stream message subtype (e.g.
system/task_progress), add mapping insrc/tools/query-consumer.ts. - If new progress events should be suppressed in minimal polling, update filtering logic in
src/tools/claude-code-check.ts.
4.4 Close the documentation loop
- Update
README.mdfor user-visible behavior changes (polling semantics, event types, defaults). - Update
docs/DESIGN.mdfor detailed mapping truth. - Update
NOTICE.mddirect dependency versions when they change. - Update
CHANGELOG.mdunderUnreleasedwith concise bullets.
4.5 Close the test loop
- Add/adjust Vitest tests whenever:
- a new SDK message is mapped/filtered
- an Option mapping changes
- a tool contract/schema changes
5) Verify (Definition of Done)
Run (in this order):
npm run typechecknpm run lintnpm testnpm run format:checknpm audit(optional; if you applynpm audit fix, re-run 1-4 and commit the lockfile change)
Also verify:
docs/DESIGN.md#sdk-interface-baseline+docs/DESIGN.md#upgrade-methodologystill reflect reality.- No new long mapping tables were added to
AGENTS.md. tmp/contains no leftover audit artifacts.
Documentation Update Pass (One Iteration)
After iterative doc refinement, complete one explicit update pass in the same branch:
- Update
AGENTS.mdfor process/policy changes only. - Update
docs/DESIGN.mdfor technical detail/mapping changes only. - Update
CHANGELOG.mdunderUnreleased -> Documentationwith a concise entry. - Re-validate cross-links:
docs/DESIGN.md#sdk-interface-baselinedocs/DESIGN.md#upgrade-methodology
- Verify no duplicated long tables drift back into
AGENTS.md.
Interface Alignment Rules
- SDK type definitions are the final authority; changelog is secondary.
- Fields directly mapped to SDK
Optionsmust keep SDK names. - Non-SDK policy/protocol fields keep project contract names.
- No long-lived compatibility aliases by default; prefer one-shot rename migration.
- Any interface change must close the full loop: schema + handlers + manager + mapping + tests + docs.
Required Change-Closure Checklist
Before merge, ensure all applicable items are updated:
src/server.tssrc/tools/*.tsrelated handlerssrc/session/manager.tssrc/utils/build-options.tssrc/tools/query-consumer.tssrc/types.tstests/*.test.tsrelated suitesREADME.mddocs/DESIGN.mdAGENTS.mdCHANGELOG.md
If a changed SDK field or message type is not reflected in at least one test, treat as incomplete.
Quick Commands
- Install deps:
npm install - Build:
npm run build - Dev watch:
npm run dev - Start server:
npm start - Typecheck:
npm run typecheck - Test:
npm test - Test watch:
npm run test:watch - Lint:
npm run lint - Format:
npm run format - Format check:
npm run format:check
Git / PR Workflow
- Base branch:
master - Keep commits focused and non-interactive
- Before commit/PR, run:
npm run typechecknpm run lintnpm testnpm run format:check
Pre-commit hook (.husky/pre-commit) runs:
npx lint-staged(prettier --write+eslint --fixfor staged*.ts)npm run typechecknpm test
Project Layout (Condensed)
src/
index.ts entry + shutdown
server.ts MCP tools + zod schema
types.ts shared types/constants
tools/
claude-code.ts
claude-code-reply.ts
claude-code-session.ts
claude-code-check.ts
query-consumer.ts
tool-discovery.ts
session/
manager.ts
utils/
build-options.ts
race-with-abort.ts
resume-token.ts
windows.ts
...
tests/
docs/
mcp_demo/
Key Dependencies
@anthropic-ai/claude-agent-sdk@modelcontextprotocol/sdkzod(v4)
Code Style & Conventions
- ESM + TS only (
"type": "module") - Local TS imports keep
.jsextension style - Prefer
unknown+ narrowing overany - zod schemas stay near tool registration in
src/server.ts - Keep zod
.describe()minimal and include default semantics:Default: <value>Default: SDKDefault: noneDefault: auto-detect 'claude', then 'claude-internal', else SDK-bundled(forpathToClaudeCodeExecutable)
Formatting source of truth:
- Prettier (
singleQuote: false, semicolons on, trailing commas ES5,printWidth: 100)
Security / Defaults
- Keep minimum-tools philosophy (do not add extra MCP tools lightly)
- Default permission mode remains
defaultwith async permission callback path - Sensitive session fields are redacted unless explicitly requested
advanced.envmerges as{ ...process.env, ...input.advanced.env }, user values take precedence- Subagent usage requires
Tasktool permission (or explicit approval path)
Environment Variables (Server Process)
CLAUDE_CODE_GIT_BASH_PATHCLAUDE_CODE_MCP_DEFAULT_CLAUDE_COMMANDCLAUDE_CODE_MCP_DEFAULT_CLAUDE_PATHCLAUDE_CODE_MCP_ALLOW_DISK_RESUMECLAUDE_CODE_MCP_RESUME_SECRETCLAUDE_CODE_MCP_MAX_SESSIONSCLAUDE_CODE_MCP_MAX_PENDING_PERMISSIONSCLAUDE_CODE_MCP_EVENT_BUFFER_MAX_SIZECLAUDE_CODE_MCP_EVENT_BUFFER_HARD_MAX_SIZE
For defaults and semantics, reference README.md and docs/DESIGN.md.
Testing Expectations
- Add/adjust Vitest tests for behavior changes
- Avoid real network calls
- Mock
@anthropic-ai/claude-agent-sdkquery()in tool tests - Use fake timers for timeout/TTL tests (
vi.useFakeTimers()+ cleanup)
Minimum suites to touch when relevant:
tests/server.test.tstests/tools.test.tstests/build-options.test.tstests/query-consumer.test.tstests/session-manager.test.tstests/claude-code-check.test.tstests/claude-code-reply.test.tstests/tool-discovery.test.tstests/resources.test.tstests/resume-token.test.ts
Build Artifacts / Publishing / CI
- Edit
src/, notdist/ npm run prepublishOnlytriggers build- Publish is public scoped package
- CI runs typecheck + lint + format:check + test + build (Node 18/20/22)
Windows Notes
- Prefer automation commands as
pwsh -NoProfile -Command "..." - Claude Code CLI requires Git Bash on Windows
- Windows-specific detection and hints live in
src/utils/windows.ts
Maintenance Principle
If AGENTS and DESIGN diverge:
- Fix DESIGN for detailed technical truth.
- Keep AGENTS concise and executable.
- Replace duplication with links to DESIGN anchors.
