|Docs

Build and Deploy Your Own MCP Server

mcptypescriptagents

The Model Context Protocol (MCP) is an open standard for connecting AI assistants to external tools and data sources. An MCP server exposes tools (functions the AI can call) and resources (data the AI can read) over a standardized protocol.

This guide covers building an MCP server in TypeScript and deploying it on Railway so AI assistants like Claude Desktop and Cursor can connect to it over the network.

Railway already has an MCP server for managing Railway infrastructure. This guide is different: it covers building and hosting your own MCP server for your own tools and data.

How MCP transport works

MCP supports two transport protocols:

  • stdio: The client launches the server as a local subprocess. Communication happens over standard input/output. This only works when the server runs on the same machine as the client.
  • SSE (Server-Sent Events): The server runs as an HTTP service. The client connects over the network. This is the transport you need for a hosted MCP server.

Since Railway hosts the server remotely, you must use SSE transport.

Prerequisites

  • A Railway account
  • Node.js 18+ for local development
  • An AI client that supports remote MCP servers (Claude Desktop, Cursor, etc.)

1. Create the MCP server

Initialize a project and install the MCP SDK:

Update tsconfig.json with the following settings:

Create the server with a sample tool:

Add a build script to package.json:

2. Test locally

The server starts on port 3000. You can test the SSE endpoint at http://localhost:3000/sse.

3. Deploy to Railway

  1. Push your code to a GitHub repository.
  2. Create a new project on Railway.
  3. Click + New > GitHub Repo and select your repository.
  4. Railway detects the Node.js project and builds it via Railpack.
  5. Generate a public domain under Settings > Networking.

Your MCP server is now reachable at https://your-server-production-xxxx.up.railway.app/sse.

4. Connect from an AI assistant

Claude Desktop

Add to your Claude Desktop configuration (claude_desktop_config.json):

Cursor

Add to your Cursor MCP settings:

Restart the client after updating the configuration. Your tools will appear in the AI assistant's tool list.

Adding authentication

A public MCP server is accessible to anyone with the URL. To restrict access, add a simple bearer token check:

Set MCP_AUTH_TOKEN as an environment variable in your Railway service.

Adding a database

If your tools need persistent state, add Postgres to your project and access it via the DATABASE_URL reference variable. For example, a tool that queries a knowledge base or stores user preferences.

Next steps