Fastify
Run an xmcp MCP server on Fastify. Works on any Node.js deployment target including AWS Lambda.
For the complete documentation index, see llms.txt. Markdown variants of every page are available by appending .md to the URL.Installation
xmcp can work on top of your existing Fastify project. To get started, run the following command in your project directory:
npx init-xmcp@latestAfter setting up the project, your build command should look like this:
{
"scripts": {
"build": "xmcp build && tsc"
}
}xmcp build bundles your tools into .xmcp/adapter.
Usage
Install Fastify and register the MCP handler on your server:
npm install fastifyimport Fastify from "fastify";
import { xmcpHandler } from "@xmcp/adapter";
const app = Fastify({ logger: false });
app.post("/mcp", xmcpHandler);
app.get("/mcp", xmcpHandler); // required for SSE / streaming clients
await app.listen({ port: 3000 });AWS Lambda
Use @fastify/aws-lambda as the Lambda bridge:
npm install fastify @fastify/aws-lambdaimport Fastify from "fastify";
import awsLambdaFastify from "@fastify/aws-lambda";
import { xmcpHandler } from "@xmcp/adapter";
const app = Fastify({ logger: false });
app.post("/mcp", xmcpHandler);
export const handler = awsLambdaFastify(app);The default buffered form (awsLambdaFastify(app)) works for all JSON-RPC POST requests (initialize, tools/call, etc.). GET/SSE is not supported in buffered mode — see the streaming form below.
SSE support on Lambda
To support SSE via GET /mcp, use a Lambda Function URL with InvokeMode: RESPONSE_STREAM and switch to the streaming form:
import Fastify from "fastify";
import awsLambdaFastify from "@fastify/aws-lambda";
import { xmcpHandler } from "@xmcp/adapter";
import { promisify } from "node:util";
import stream from "node:stream";
const pipeline = promisify(stream.pipeline);
const app = Fastify({ logger: false });
app.post("/mcp", xmcpHandler);
app.get("/mcp", xmcpHandler);
const proxy = awsLambdaFastify(app, { payloadAsStream: true });
export const handler = awslambda.streamifyResponse(
async (event, responseStream, context) => {
const { meta, stream: bodyStream } = await proxy(event, context);
responseStream = awslambda.HttpResponseStream.from(responseStream, meta);
await pipeline(bodyStream, responseStream);
}
);xmcp.config.ts
import type { XmcpConfig } from "xmcp";
const config: XmcpConfig = {
http: true,
experimental: {
adapter: "fastify",
},
};
export default config;