К основному содержанию
правило официальный · GitHub

typescript-mcp-server — правило для GitHub Copilot

Правило подсказывает Copilot, как писать MCP-серверы на TypeScript SDK: классы, схемы на zod, транспорты и типовые ошибки.

Установить в GitHub Copilot · Вручную: положить файлы Все способы и платформы ↓
.github/instructions/typescript-mcp-server.instructions.md
Открыть на GitHub

Что делает typescript-mcp-server

Файл-инструкция подключается к рабочему пространству и срабатывает на **/*.ts, **/*.js и **/package.json. Внутри — набор указаний по @modelcontextprotocol/sdk: брать класс McpServer для высокоуровневой реализации с автоматической обработкой протокола или Server, если нужны ручные обработчики запросов. Схемы входа и выхода описываются через zod, а регистрация идёт методами registerTool(), registerResource() и registerPrompt().

Отдельный блок про транспорты. Для HTTP-серверов правило советует StreamableHTTPServerTransport вместе с Express, для локальных интеграций — StdioServerTransport. В stateless-режиме транспорт создаётся заново на каждый запрос, иначе идентификаторы запросов столкнутся; для серверов с состоянием нужен sessionIdGenerator. Локальным серверам предписана защита от DNS rebinding через enableDnsRebindingProtection: true.

Есть раздел с примерами: базовый сервер на HTTP и на stdio, простой инструмент с калькулятором, динамический ресурс через ResourceTemplate, инструмент с sampling через server.server.createMessage(), промпт с автодополнением. Проверять серверы предлагается через npx @modelcontextprotocol/inspector. Правило требует ставить пакеты явно — npm install @modelcontextprotocol/sdk и npm install zod@3 — так что без них код из примеров не соберётся.

Какие задачи решает

  • поднять MCP-сервер на TypeScript с нуля
  • описать схемы инструментов через zod вместо ручной валидации
  • выбрать между stdio и HTTP-транспортом для конкретной задачи
  • настроить сессии и защиту от DNS rebinding у локального сервера
  • запросить у клиента дополнительный ввод прямо во время выполнения инструмента

Примеры запросов агенту

  • Напиши MCP-сервер на TypeScript с инструментом поиска по документации
  • Переделай этот сервер на StreamableHTTPServerTransport с Express
  • Добавь динамический ресурс users://{userId} через ResourceTemplate

Как установить typescript-mcp-server

Вручную: положить файлы

.github/instructions/typescript-mcp-server.instructions.md

Поле applyTo задаёт, к каким файлам применять

По шагам, с командами для каждой платформы: AGENTS.md и CLAUDE.md: что это, кто читает и какой выбрать

Вопросы о typescript-mcp-server

Как установить typescript-mcp-server в GitHub Copilot?

Скопируйте файл правила в .github/instructions/typescript-mcp-server.instructions.md в корне репозитория. После этого Copilot будет подхватывать инструкции для файлов .ts, .js и package.json.

Что делает typescript-mcp-server?

Задаёт Copilot правила написания MCP-серверов на TypeScript SDK: какие классы и методы использовать, как описывать схемы и как настраивать транспорты.

Какие пакеты нужны для работы по этому правилу?

Правило опирается на @modelcontextprotocol/sdk и zod@3 — ставятся командами npm install @modelcontextprotocol/sdk и npm install zod@3.

Исходный текст

typescript-mcp-server.instructions.md · MIT
---
description: 'Instructions for building Model Context Protocol (MCP) servers using the TypeScript SDK'
applyTo: '**/*.ts, **/*.js, **/package.json'
---

# TypeScript MCP Server Development

## Instructions

- Use the **@modelcontextprotocol/sdk** npm package: `npm install @modelcontextprotocol/sdk`
- Import from specific paths: `@modelcontextprotocol/sdk/server/mcp.js`, `@modelcontextprotocol/sdk/server/stdio.js`, etc.
- Use `McpServer` class for high-level server implementation with automatic protocol handling
- Use `Server` class for low-level control with manual request handlers
- Use **zod** for input/output schema validation: `npm install zod@3`
- Always provide `title` field for tools, resources, and prompts for better UI display
- Use `registerTool()`, `registerResource()`, and `registerPrompt()` methods (recommended over older APIs)
- Define schemas using zod: `{ inputSchema: { param: z.string() }, outputSchema: { result: z.string() } }`
- Return both `content` (for display) and `structuredContent` (for structured data) from tools
- For HTTP servers, use `StreamableHTTPServerTransport` with Express or similar frameworks
- For local integrations, use `StdioServerTransport` for stdio-based communication
- Create new transport instances per request to prevent request ID collisions (stateless mode)
- Use session management with `sessionIdGenerator` for stateful servers
- Enable DNS rebinding protection for local servers: `enableDnsRebindingProtection: true`
- Configure CORS headers and expose `Mcp-Session-Id` for browser-based clients
- Use `ResourceTemplate` for dynamic resources with URI parameters: `new ResourceTemplate('resource://{param}', { list: undefined })`
- Support completions for better UX using `completable()` wrapper from `@modelcontextprotocol/sdk/server/completable.js`
- Implement sampling with `server.server.createMessage()` to request LLM completions from clients
- Use `server.server.elicitInput()` to request additional user input during tool execution
- Enable notification debouncing for bulk updates: `debouncedNotificationMethods: ['notifications/tools/list_changed']`
- Dynamic updates: call `.enable()`, `.disable()`, `.update()`, or `.remove()` on registered items to emit `listChanged` notifications
- Use `getDisplayName()` from `@modelcontextprotocol/sdk/shared/metadataUtils.js` for UI display names
- Test servers with MCP Inspector: `npx @modelcontextprotocol/inspector`

## Best Practices

- Keep tool implementations focused on single responsibilities
- Provide clear, descriptive titles and descriptions for LLM understanding
- Use proper TypeScript types for all parameters and return values
- Implement comprehensive error handling with try-catch blocks
- Return `isError: true` in tool results for error conditions
- Use async/await for all asynchronous operations
- Close database connections and clean up resources properly
- Validate input parameters before processing
- Use structured logging for debugging without polluting stdout/stderr
- Consider security implications when exposing file system or network access
- Implement proper resource cleanup on transport close events
- Use environment variables for configuration (ports, API keys, etc.)
- Document tool capabilities and limitations clearly
- Test with multiple clients to ensure compatibility

## Common Patterns

### Basic Server Setup (HTTP)
```typescript
import { McpServer } from '@modelcontextprotocol/sdk/server/mcp.js';
import { StreamableHTTPServerTransport } from '@modelcontextprotocol/sdk/server/streamableHttp.js';
import express from 'express';

const server = new McpServer({
    name: 'my-server',
    version: '1.0.0'
});

const app = express();
app.use(express.json());

app.post('/mcp', async (req, res) => {
    const transport = new StreamableHTTPServerTransport({
        sessionIdGenerator: undefined,
        enableJsonResponse: true
    });
    
    res.on('close', () => transport.close());
    
    await server.connect(transport);
    await transport.handleRequest(req, res, req.body);
});

app.listen(3000);
```

### Basic Server Setup (stdio)
```typescript
import { McpServer } from '@modelcontextprotocol/sdk/server/mcp.js';
import { StdioServerTransport } from '@modelcontextprotocol/sdk/server/stdio.js';

const server = new McpServer({
    name: 'my-server',
    version: '1.0.0'
});

// ... register tools, resources, prompts ...

const transport = new StdioServerTransport();
await server.connect(transport);
```

### Simple Tool
```typescript
import { z } from 'zod';

server.registerTool(
    'calculate',
    {
        title: 'Calculator',
        description: 'Perform basic calculations',
        inputSchema: { a: z.number(), b: z.number(), op: z.enum(['+', '-', '*', '/']) },
        outputSchema: { result: z.number() }
    },
    async ({ a, b, op }) => {
        const result = op === '+' ? a + b : op === '-' ? a - b : 
                      op === '*' ? a * b : a / b;
        const output = { result };
        return {
            content: [{ type: 'text', text: JSON.stringify(output) }],
            structuredContent: output
        };
    }
);
```

### Dynamic Resource
```typescript
import { ResourceTemplate } from '@modelcontextprotocol/sdk/server/mcp.js';

server.registerResource(
    'user',
    new ResourceTemplate('users://{userId}', { list: undefined }),
    {
        title: 'User Profile',
        description: 'Fetch user profile data'
    },
    async (uri, { userId }) => ({
        contents: [{
            uri: uri.href,
            text: `User ${userId} data here`
        }]
    })
);
```

### Tool with Sampling
```typescript
server.registerTool(
    'summarize',
    {
        title: 'Text Summarizer',
        description: 'Summarize text using LLM',
        inputSchema: { text: z.string() },
        outputSchema: { summary: z.string() }
    },
    async ({ text }) => {
        const response = await server.server.createMessage({
            messages: [{
                role: 'user',
                content: { type: 'text', text: `Summarize: ${text}` }
            }],
            maxTokens: 500
        });
        
        const summary = response.content.type === 'text' ? 
            response.content.text : 'Unable to summarize';
        const output = { summary };
        return {
            content: [{ type: 'text', text: JSON.stringify(output) }],
            structuredContent: output
        };
    }
);
```

### Prompt with Completion
```typescript
import { completable } from '@modelcontextprotocol/sdk/server/completable.js';

server.registerPrompt(
    'review',
    {
        title: 'Code Review',
        description: 'Review code with specific focus',
        argsSchema: {
            language: completable(z.string(), value => 
                ['typescript', 'python', 'javascript', 'java']
                    .filter(l => l.startsWith(value))
            ),
            code: z.string()
        }
    },
    ({ language, code }) => ({
        messages: [{
            role: 'user',
            content: {
                type: 'text',
                text: `Review this ${language} code:\n\n${code}`
            }
        }]
    })
);
```

### Error Handling
```typescript
server.registerTool(
    'risky-operation',
    {
        title: 'Risky Operation',
        description: 'An operation that might fail',
        inputSchema: { input: z.string() },
        outputSchema: { result: z.string() }
    },
    async ({ input }) => {
        try {
            const result = await performRiskyOperation(input);
            const output = { result };
            return {
                content: [{ type: 'text', text: JSON.stringify(output) }],
                structuredContent: output
            };
        } catch (err: unknown) {
            const error = err as Error;
            return {
                content: [{ type: 'text', text: `Error: ${error.message}` }],
                isError: true
            };
        }
    }
);
```

© github · MIT · github/awesome-copilot

Похожие правила IDE

Все правила IDE по теме «Настройка агента: память, контекст, субагенты» →

Из того же набора928

Весь набор →
use-cliche-data-in-docs Правило помогает писать документацию и примеры с вымышленными данными вместо значений из конфигов, скриптов и запросов. Пригодится при создании и правке документации. ★ 40 418звёзд репо update-docs-on-code-change Правило для GitHub Copilot, которое напоминает обновлять README.md и документацию, когда меняется код. Пригодится, если доки у вас отстают от реальности. ★ 40 418звёзд репо update-code-from-shorthand Превращает псевдокод и краткие заметки между маркерами в готовый код. Помогает быстро развернуть черновую логику прямо внутри целевого файла. ★ 40 418звёзд репо typespec-m365-copilot Правило подсказывает, как описывать агентов и API-плагины Microsoft 365 Copilot на TypeSpec. Срабатывает при работе с файлами .tsp. ★ 40 418звёзд репо terraform Набор инструкций для GitHub Copilot, который задаёт стандарты написания манифестов **/*.tf. Он следит за безопасностью секретов, структурой модулей и форматированием кода. ★ 40 418звёзд репо terraform-sap-btp Правило задаёт рекомендации для Terraform-кода в SAP BTP: от структуры модулей до управления состоянием. Пригодится при создании и проверке инфраструктурных конфигураций. ★ 40 418звёзд репо terraform-azure Правило задаёт Copilot стиль и ограничения при работе с Terraform на Azure: структура файлов, модули AVM, запрет на секреты в состоянии. ★ 40 418звёзд репо tasksync Держит сессию агента активной и запрашивает новые команды через терминал с помощью Python после каждого шага. ★ 40 418звёзд репо

Спонсоры проекта

Компании, которые помогают держать аналитику открытой