Skip to content

Building Custom MCP Servers

While the public ecosystem of MCP servers is vast, the ultimate power of the Model Context Protocol lies in its extensibility. Every organization has its own unique set of internal tools, APIs, and documentation. By building custom MCP servers, you can give your AI assistant direct, secure access to this proprietary knowledge, unlocking a level of tailored automation that is simply not possible with off-the-shelf tools.

This guide provides a high-level overview of the process and principles for building your own MCP server.

Building an MCP server is surprisingly straightforward, thanks to the open protocol and available SDKs. The process involves defining a set of “tools” that your AI can use.

1. Choose Your Stack

You can write an MCP server in any language that can communicate over stdio (for local tools) or HTTP/SSE (for remote services). Official SDKs for TypeScript and Python make the process particularly easy.

2. Define Your Tools

A “tool” is a specific capability you want to expose to the AI. Each tool has a name, a schema for its expected inputs (often defined with a library like Zod), and a function that contains the actual logic.

3. Implement the Logic

The tool’s function is where you integrate with your internal system. This could involve making a fetch call to a private API, querying an internal database, or scraping a company wiki.

4. Connect and Run

Finally, you instantiate your server, connect it to a transport (like StdioServerTransport for a command-line tool), and it’s ready to be used by your AI assistant.


Example: A Custom Server for Internal Documentation

Section titled “Example: A Custom Server for Internal Documentation”

Let’s imagine you want to build a simple server that can scrape your company’s internal Confluence wiki.

  1. Set Up the Project. You’d start a new Node.js project, installing the MCP TypeScript SDK (@modelcontextprotocol/sdk) and a library for HTML-to-Markdown conversion, like turndown.

  2. Define the get_doc Tool. You would create a tool named get_doc. Its input schema would expect a single string parameter: url.

  3. Implement the Scraping Logic. The tool’s function would take the url, use fetch to get the HTML content of the Confluence page, and then use the turndown service to convert the HTML into clean Markdown suitable for the AI.

  4. Register and Run the Server. You’d add the tool to a new McpServer instance and connect it to the StdioServerTransport. Now, you can add this local server to your AI assistant’s configuration.

Example TypeScript code for a custom MCP server
import { McpServer } from "@modelcontextprotocol/sdk/server/mcp.js";
import { StdioServerTransport } from "@modelcontextprotocol/sdk/server/stdio.js";
import { z } from "zod";
import TurndownService from "turndown";
// 1. Create an MCP server instance
const server = new McpServer({
name: "internal-docs",
version: "1.0.0"
});
const turndownService = new TurndownService();
// 2. Add a tool to scrape internal documentation
server.tool("get_doc",
{ url: z.string() },
async ({ url }) => {
// 3. Implement the tool's logic
try {
const response = await fetch(url);
const html = await response.text();
const markdown = turndownService.turndown(html);
return {
content: [{ type: "text", text: markdown }]
};
} catch (error) {
// Handle errors gracefully
return {
content: [{ type: "text", text: `Error scraping ${url}: ${error.message}` }]
};
}
}
);
// 4. Start the server
const transport = new StdioServerTransport();
await server.connect(transport);

By building custom MCP servers, you can create a hyper-personalized AI assistant that understands the unique context of your organization, making it an even more powerful and indispensable part of your team.