Validate MCP Tool Inputs with JSON Schema and Return Protocol Errors
Intermediate · 30 minutes · 5 tasks, checked automatically · 100 points
In this lab you will build a Model Context Protocol (MCP) tool server in Node.js that enforces strict JSON Schema validation on every tool call. When a caller passes bad arguments the server must respond with a well-formed MCP protocol error — not a crash, not a silent failure. You will use the MCP TypeScript SDK, wire up Ajv for schema validation, exercise the server with the MCP Inspector CLI, and finally drive it from a small client script that confirms correct error codes are returned.
What you’ll learn
- Scaffold a Node.js MCP server using the @modelcontextprotocol/sdk package
- Attach a JSON Schema to each registered tool and validate incoming arguments with Ajv before execution
- Return a correctly structured MCP InvalidParams error when validation fails, and verify the behavior with the Inspector CLI and a client script
How this lab works
- Start the lab: One click starts a sandbox with a local model and the agent’s toolbox: no API key and no token bill.
- Work through 5 tasks: Each task says what to build or fix. You do it yourself, in a real terminal, with hints when you get stuck.
- Check your work: Check my work inspects the agent, its config and its output, and says what passed and what to fix.
- Finish and keep going: 100 points, your progress saved, and the next lab to try.
What it runs on
- Runs: Locally, against a model on your computer: no API key, no tokens
- Model: qwen2.5:3b
- Open source: github.com/modelcontextprotocol
What you need
- A free LearnXops account
- The LearnXops desktop app for macOS, Windows or Linux
- Docker Desktop (or Podman / Rancher Desktop) installed and running
- A model sandbox started in the Agent Playground
Topics: mcp, json-schema, ajv, tool-validation, protocol-errors, nodejs, stdio-server, integration
Create a free account to start this lab.
Keep going
- Connect Hermes to an MCP Server and Call a Tool
- MCP Prompts with Arguments: Build a Parameterised Workflow Server
- Wrap a Local Database as an MCP Server with Read-Only Query Tools and a Schema Resource
- Wire an MCP Server into a Claude Agent Loop
- Expose MCP Resources and Tools, Then Fetch a Resource by URI
- Read the MCP Handshake: Initialize, Capability Negotiation, and Tool Listing