QA
Intermediate LevelAI, LLMs, Embeddings & MCP

How do you build a custom MCP Server using @modelcontextprotocol/sdk in TypeScript?

❓ Interview Prompt: Explain MCP Server class instantiation, registering ListTools and CallTool handlers, and error handling.

Building a custom MCP Server using the official @modelcontextprotocol/sdk involves registering capability handlers and connecting to a transport layer.

Key Implementation Steps:

1
Instantiate Server: Declare server metadata and capabilities.
2
Register Tool List: Handle ListToolsRequestSchema by returning JSON Schema function declarations.
3
Register Tool Call Handler: Handle CallToolRequestSchema by executing requested tool logic and returning content blocks.
4
Connect Transport: Bind server to StdioServerTransport.

Code Implementation Example

typescript
import { Server } from '@modelcontextprotocol/sdk/server/index.js';
import { StdioServerTransport } from '@modelcontextprotocol/sdk/server/stdio.js';
import { ListToolsRequestSchema, CallToolRequestSchema } from '@modelcontextprotocol/sdk/types.js';

const server = new Server({ name: 'calculator-mcp', version: '1.0.0' }, { capabilities: { tools: {} } });

// 1. Expose tool schema list
server.setRequestHandler(ListToolsRequestSchema, async () => ({
  tools: [{
    name: 'add',
    description: 'Add two numbers',
    inputSchema: {
      type: 'object',
      properties: { a: { type: 'number' }, b: { type: 'number' } },
      required: ['a', 'b']
    }
  }]
}));

// 2. Handle tool calls
server.setRequestHandler(CallToolRequestSchema, async (request) => {
  if (request.params.name === 'add') {
    const { a, b } = request.params.arguments as { a: number; b: number };
    return { content: [{ type: 'text', text: String(a + b) }] };
  }
  throw new Error('Tool not found');
});

await server.connect(new StdioServerTransport());
💡Complete TypeScript custom MCP Server exposing an add tool.
🎯 Interview Key Takeaway

Use @modelcontextprotocol/sdk to register ListTools and CallTool handlers bound to StdioServerTransport.

#MCP#TypeScript#SDK#Tools#Intermediate
Back to all AI, LLMs, Embeddings & MCP Questions→

More AI, LLMs, Embeddings & MCP Interview Questions

Beginner LevelAI, LLMs, Embeddings & MCP

What are Vector Embeddings and Cosine Similarity in AI Semantic Search?

Beginner LevelAI, LLMs, Embeddings & MCP

What is the Model Context Protocol (MCP) and how does it connect LLMs to external tools?

Beginner LevelAI, LLMs, Embeddings & MCP

What is Retrieval-Augmented Generation (RAG) and how does it prevent LLM hallucinations?