How to Build MCP Servers with TypeScript: A Developer's Guide
The MCP TypeScript SDK lets web and Node.js developers build servers that follow the Model Context Protocol, connecting cloud data to AI agents. This guide covers setup, tool creation, and serverless deployment strategies.
What is the MCP TypeScript SDK?
The Model Context Protocol (MCP) TypeScript SDK is the official library for building MCP servers and clients in JavaScript and TypeScript environments. It provides a typed, standards-compliant foundation for exposing data resources, prompts, and executable tools to AI agents like Claude or Fastio's automated assistants. The protocol connects LLMs to real-world data sources through a standard interface. The SDK handles the JSON-RPC message passing protocol, managing the handshake and connection lifecycle automatically. This lets developers focus on defining their business logic and data schemas. Whether you're connecting a local PostgreSQL database, a legacy enterprise system, or a modern cloud storage API, the TypeScript SDK makes sure your server communicates correctly with any MCP-compliant client.
Helpful references: Fastio Workspaces, Fastio Collaboration, and Fastio AI.
Why Use TypeScript for MCP Servers?
TypeScript has become the standard for modern AI tooling development, balancing developer speed with application stability. When building MCP servers, type safety helps prevent subtle runtime errors in protocol communication. This matters when AI agents are autonomously calling your tools and interpreting the results.
Key Benefits:
- Type Safety: The SDK exports strict types for
CallToolRequest,ListResourcesResult, and other core protocol messages, so your implementation matches the spec. - Ecosystem: Node.js has the largest ecosystem of libraries for connecting to third-party APIs, relational databases, and complex file systems. This makes it a strong choice for integration work.
- Isomorphic Code: You can share validation logic, interface definitions, and utility functions between your MCP server and your existing web or mobile applications.
- Developer Experience: With first-class IDE support, building MCP servers in TypeScript gives you autocompletion for all protocol methods, reducing the learning curve for new developers.
Prerequisites and Project Setup
Before building your server, make sure you have Node.js (v18+) and npm installed. We'll create a new project that can run as a local process or a serverless function.
Step 1: Initialize the Project
mkdir my-mcp-server
cd my-mcp-server
npm init -y
npm install typescript @types/node --save-dev
npx tsc --init
Step 2: Install the MCP SDK
npm install @modelcontextprotocol/sdk zod
We include zod for runtime schema validation of tool arguments, which is a best practice for safe agent interactions. Getting started should be straightforward. A good platform lets you create an account, invite your team, and start uploading files within minutes, not days. Avoid tools that require complex server configuration or IT department involvement just to get running.
Building Your First MCP Server
Here is a boilerplate implementation of a TypeScript MCP server. This example exposes a simple tool that an AI agent can call to calculate storage costs.
Create src/index.ts:
import { Server } from "@modelcontextprotocol/sdk/server/index.js";
import { StdioServerTransport } from "@modelcontextprotocol/sdk/server/stdio.js";
import { CallToolRequestSchema, ListToolsRequestSchema } from "@modelcontextprotocol/sdk/types.js";
import { z } from "zod";
// 1. Initialize the Server
const server = new Server(
{
name: "storage-calculator",
version: "1.0.0",
},
{
capabilities: {
tools: {},
},
}
);
// 2. Define Tools
server.setRequestHandler(ListToolsRequestSchema, async () => {
return {
tools: [
{
name: "calculate_storage_cost",
description: "Calculate the monthly cost for cloud storage",
inputSchema: {
type: "object",
properties: {
gigabytes: { type: "number", description: "Amount of storage in GB" },
provider: { type: "string", enum: ["standard", "archive"] },
},
required: ["gigabytes"],
},
},
],
};
});
// 3. Handle Tool Execution
server.setRequestHandler(CallToolRequestSchema, async (request) => {
if (request.params.name === "calculate_storage_cost") {
const args = request.params.arguments as any;
const rate = args.provider === "archive" ? 0.01 : 0.023;
const cost = args.gigabytes * rate;
return {
content: [
{
type: "text",
text: `Estimated cost: $${cost.toFixed(2)} per month`,
},
],
};
}
throw new Error("Tool not found");
});
// 4. Start the Server
const transport = new StdioServerTransport();
await server.connect(transport);
console.error("MCP Server running on stdio");
Give Your AI Agents Persistent Storage
Skip the boilerplate. Fastio gives you a fully managed MCP server with 251 tools, 50GB of storage, and zero configuration.
Deploying MCP as Serverless Functions
While local stdio servers work well for personal development and private use cases (like with Claude Desktop), production MCP servers often need to run in the cloud to be accessible to distributed agents. You can deploy your TypeScript MCP server as a serverless function on platforms like Vercel, AWS Lambda, or Cloudflare Workers using the SSE (Server-Sent Events) transport layer.
Why Serverless?
- Cost Efficiency: You only pay for compute resources when an agent queries your server, which saves money for low-traffic integrations.
- Global Scalability: These platforms automatically handle concurrent requests from multiple agents, scaling up or down based on demand.
- Better Security: Your server runs in an isolated, ephemeral environment, reducing the attack surface and removing the need for long-term server maintenance. To support this cloud-native architecture, replace the standard
StdioServerTransportwithSSEServerTransportand wrap your server instance in an HTTP handler that works with your chosen cloud provider's request/response format.
Connecting Storage APIs to MCP
One common use case for MCP is giving AI agents direct access to file storage and document management systems. Instead of building your own file handling logic, you can connect your MCP server to existing storage APIs. For example, Fastio provides a built-in MCP server for its users, letting them expose their cloud files to LLMs without extra setup. Fastio's agents come equipped with 251 pre-built tools for file manipulation, metadata searching, and secure sharing across teams. If you're building a custom integration to process proprietary file formats before storage, you can use the TypeScript SDK to fetch files, process them in memory, and then upload them to a destination using standard fetch APIs within your tool logic. This lets you build specialized AI tools for your specific data workflows.
Frequently Asked Questions
How do I test my TypeScript MCP server locally?
You can test your server using the 'MCP Inspector' tool provided by the SDK maintainers, or by configuring Claude Desktop to run your `build/index.js` file via the `node` command in its configuration file. This allows you to chat with your local server immediately.
Can I use MCP with Python instead of TypeScript?
Yes, there is an official Python SDK for MCP that offers similar functionality. However, the TypeScript SDK is often preferred for web-native integrations or when running on JavaScript-heavy serverless platforms like Cloudflare Workers.
Is the MCP TypeScript SDK compatible with Next.js?
Yes, the SDK runs in Next.js API routes. Since Next.js uses Node.js (or the Edge runtime), you can define an API route that acts as an MCP server endpoint using the SSE transport, making your application's data accessible to agents.
Related Resources
Give Your AI Agents Persistent Storage
Skip the boilerplate. Fastio gives you a fully managed MCP server with 251 tools, 50GB of storage, and zero configuration.