taliawedged117/census-mcp-server/api-testing
Claude Code and agent skill by taliawedged117/census-mcp-server
- community
Checking that the SKILL.md file is still in the repository.
Testing patterns for MCP tool/resource handlers using `createMockContext` and Vitest. Covers mock context options, handler testing, McpError assertions, format testing, Vitest config setup, and test isolation conventions.
Install
npx vskill@latest install taliawedged117/census-mcp-server/api-testingSKILL.md
The same SKILL.md was published earlier as ficaviolaodorata520/met-museum-mcp-server/api-testing.
Overview
Tests target handler behavior directly — call handler(input, ctx), assert on the return value or thrown error. The framework's handler factory (try/catch, formatting, telemetry) is not involved. Use createMockContext from @cyanheads/mcp-ts-core/testing to construct the ctx argument.
Additional exports from `/testing`: createMockLogger() returns a standalone MockContextLogger for unit-testing code that accepts a ContextLogger directly (services, utilities). createInMemoryStorage(options?) provides a real StorageService backed by InMemoryProvider for testing services that take a StorageService dependency.
Philosophy: Test behavior, not implementation. Refactors should not break tests. Match the repo's existing test layout: fresh scaffolds use tests/, while colocated src/**/*.test.ts files are also supported. Integration tests at I/O boundaries over unit tests of internals.
createMockContext options
MockContextOptions interface:
| Option | Effect | |
|---|---|---|
| _(none)_ | Minimal context — ctx.state operations throw without tenantId; ctx.elicit/ctx.sample/ctx.progress are undefined | |
auth | Sets ctx.auth for scope-checking tests | |
elicit | Assigns a function to ctx.elicit for testing elicitation calls | |
errors | Attaches a typed ctx.fail against the contract — same wiring the production handler factory uses. Pass myTool.errors directly. | |
notifyPromptListChanged | Assigns ctx.notifyPromptListChanged for prompt-list change notification tests | |
notifyResourceListChanged | Assigns ctx.notifyResourceListChanged for resource notification tests | |
notifyResourceUpdated | Assigns ctx.notifyResourceUpdated for resource update notification tests | |
notifyToolListChanged | Assigns ctx.notifyToolListChanged for tool-list change notification tests | |
sessionId | Sets ctx.sessionId for handlers that branch on session ID | |
progress | Populates ctx.progress with real state-tracking implementation (see below) | |
requestId | Overrides ctx.requestId (default: 'test-request-id') | |
sample | Assigns a function to ctx.sample for testing sampling calls | |
signal | Overrides ctx.signal — useful for cancellation testing | |
tenantId | Sets ctx.tenantId and enables ctx.state operations with in-memory storage | |
uri | Sets ctx.uri for resource handler testing |
Mock progress
When progress: true, ctx.progress is a real state-tracking object — not vi.fn() spies. It maintains internal state accessible via inspection properties:
Mock logger
ctx.log captures all log calls for inspection. Import MockContextLogger from @cyanheads/mcp-ts-core/testing and cast ctx.log to access the .calls array (the cast is necessary because createMockContext returns Context, which types log as ContextLogger):
Full test example
Parse input through myTool.input.parse(...) to validate against the Zod schema and produce the typed input the handler expects. Call myTool.handler(input, ctx) directly, not through the MCP SDK or any framework wrapper. Assert on the return value for happy paths; use .rejects.toThrow() for error paths. Test format separately if the tool defines one — it's a pure function and needs no ctx. Verify the rendered text includes the fields the LLM needs, and for projection-style tools, add a case with non-default field selections.
Testing with optional capabilities
Testing with form-based client payloads
LLM clients only send populated fields. Form-based clients (MCP Inspector, web UIs) submit the full schema shape — optional object fields arrive with empty-string inner values instead of undefined. Both are valid MCP usage. Test that handlers handle both gracefully.
The pattern: parse through the schema (confirms Zod accepts the payload), call the handler, assert the empty-value case produces correct results — no errors, no corrupted downstream queries. Same applies to optional arrays: test with [] to verify the handler skips rather than passes through.
Testing with sparse upstream payloads
This is a different problem from form-client '' payloads. Here the upstream API omits fields entirely. The risk is either a validation failure from an over-strict schema or a quiet lie where missing data turns into a concrete fact.
What to verify:
- Fixtures omit fields entirely, not just set them to
nullor''. - Normalization/helpers tolerate missing fields without fabricating defaults.
- Handler output still validates against the declared output schema.
format()uses explicit unknown-state fallbacks instead of inventing facts.- Tool-semantic defaults are tested separately from upstream absence so the distinction stays clear.
Vitest config
Extend the framework's base config using mergeConfig. The base provides globals: true, pool: 'forks', isolate: true, tsconfigPaths, and a Zod SSR compatibility fix. Add only the @/ alias for your server's source:
This is the start of the file. Read the full SKILL.md on GitHub.
Security
- Security scanPassed, 90 of 100Pattern checks on the skill's files.
- Model reviewPassed
Quality evaluation
- Task evaluationNot runNo evaluation yet. Run one in Skill Studio.
Source
- SKILL.md in the repositoryCheckingskills/api-testing/SKILL.md
Popularity
- 7-day trend score
- 0
Not refreshed yet.
Works with
Any agent vskill installs to: 56 targets in all. These 39 have a page on this site.
- Claude Code
- Cursor
- GitHub Copilot
- Windsurf
- Codex
- Gemini CLI
- Cline
- Amp
- Roo Code
- Goose
- Continue
- Kilo Code
- Junie
- Trae
- OpenHands
- Qwen Code
- AI Agent
- Tabnine Chat
- Sourcegraph Cody
- Amazon Q
- Firebase Genkit
- Sweep
- Devin
- Phind
- Mentat
- Claude Desktop
- Aider
- GPT Pilot
- Plandex
- bolt.new
- v0
- Replit Agent
- Anse
- TypingMind
- BetterChatGPT
- LibreChat
- Warp AI
- Pieces
- Fig AI
Embed badge
[](https://verified-skill.com/skills/taliawedged117/census-mcp-server/api-testing)