# MCP Server

> The MCP Server is a program that exposes tools, resources and prompts to AI applications through the Model Context Protocol.

## How do I build an MCP server?

The program that provides capabilities to AI applications via MCP: tools that perform actions, resources with data and prompt templates. It can run locally or remotely, for example to give an agent access to a database, docs or a design tool.

- Topic: Model Context Protocol
- Type: concept

## Example

Wrong:

```ts
server.tool('search_docs', { query: z.string() }, async ({ query }) => {
  console.log('searching', query);
  return { content: [{ type: 'text', text: await search(query) }] };
});
```

Right:

```ts
server.tool('search_docs', { query: z.string().min(1).max(200) }, async ({ query }) => {
  console.error('searching', query);
  return { content: [{ type: 'text', text: await search(query) }] };
});
```

A stdio server must log to stderr, because stdout carries protocol messages, and inputs get real limits.

## Common mistakes

- Printing logs to stdout and breaking the stdio protocol.

## Key points

- Every tool input is validated on the server, whatever the model sends.
- Secrets come from environment variables, not from code or tool arguments.
- A local stdio server writes logs to stderr, never to stdout.
- Remote servers require authentication and check access on each request.

## Interview questions

### Should my MCP server run locally or remotely?

Local stdio servers suit tools that need the user's files or machine. Remote HTTP servers suit shared services and need authentication.

### How do I test an MCP server?

Use an MCP inspector tool or a test client to list tools and call them with sample inputs, and write unit tests for the tool handlers.

### How many tools should one MCP server have?

Keep it focused. Too many similar tools confuse the model and fill the context, so group related actions and give each tool a clear description.

## Related cards

- [MCP](https://wonderium.dev/ai/mcp.md)
- [MCP Tool](https://wonderium.dev/ai/mcp-tool.md)
- [MCP Resource](https://wonderium.dev/ai/mcp-resource.md)
- [MCP Transport](https://wonderium.dev/ai/mcp-transport.md)
- [JSON Schema](https://wonderium.dev/ai/json-schema.md)

---

- Level: [Artificial Intelligence](https://wonderium.dev/ai.md)
- HTML version: https://wonderium.dev/ai/mcp-server
- Last updated: 2026-11-25
