Skip to main content
Glama
sarreche

simple-mcp-basics

by sarreche

MCP básico con TypeScript

Ejemplo pequeño pero completo de Model Context Protocol (MCP): un servidor local, un cliente y una demostración ejecutable de las tres primitivas principales del servidor.

  • Tools: acciones que un modelo puede decidir invocar.

  • Resources: datos de solo lectura que una aplicación cliente obtiene por URI.

  • Prompts: plantillas de mensajes que el usuario o cliente puede seleccionar.

El proyecto evita deliberadamente bases de datos, frameworks web, autenticación y llamadas a un LLM. El objetivo es que se vea el protocolo sin ruido adicional y que el código pueda servir como punto de partida.

Requisitos

  • Node.js 20 o posterior

  • npm

Related MCP server: MCP AI Chat LangChain

Ejecutar la demo

npm install
npm run demo

No hay que iniciar el servidor aparte. El cliente lo lanza como subproceso, se conecta por stdio, hace el handshake MCP y lo cierra al terminar.

La salida incluye, en este orden:

  1. Descubrimiento de sumar mediante listTools() y ejecución con callTool().

  2. Descubrimiento de info://app mediante listResources() y lectura con readResource().

  3. Descubrimiento de explicar-concepto mediante listPrompts() y renderizado con getPrompt().

También se puede comprobar el tipado y generar JavaScript:

npm run check
npm run build

Mapa del proyecto

src/
├── server.ts  # declara tools, resources y prompts; atiende por stdio
└── client.ts  # inicia el server, descubre capacidades y las consume

Qué ocurre al ejecutar el cliente

npm run demo
     │
     ▼
Client + StdioClientTransport
     │  crea el proceso y usa stdin/stdout
     ▼
McpServer + transporte stdio
     │
     ├── tools/list      ──► definición y JSON Schema de `sumar`
     ├── tools/call      ──► ejecuta `sumar`
     ├── resources/list  ──► metadatos de `info://app`
     ├── resources/read  ──► contenido del resource
     ├── prompts/list    ──► metadatos de `explicar-concepto`
     └── prompts/get     ──► mensajes de la plantilla

client.connect(transport) realiza primero el handshake initialize: cliente y servidor intercambian versión, identidad y capacidades. Después, el SDK presenta métodos TypeScript de alto nivel; por debajo viajan mensajes JSON-RPC de MCP.

Tool

sumar ilustra una operación. Su esquema Zod tiene tres trabajos: documenta los argumentos, genera el JSON Schema que descubre el cliente y valida el input antes de ejecutar el handler.

En un sistema real, un tool podría consultar una API, crear un ticket o ejecutar una operación de negocio. Debe tener un nombre estable, una descripción precisa y un esquema estricto.

Resource

info://app ilustra información direccionable y de solo lectura. La lista contiene metadatos; el contenido se obtiene en una llamada separada usando la URI.

En un sistema real, un resource podría representar documentación, configuración, el esquema de una base de datos o un registro. Para colecciones variables, el SDK también ofrece ResourceTemplate con URIs como customer://{id}.

Prompt

explicar-concepto recibe argumentos y produce mensajes. No llama por sí mismo a un LLM: un host con un modelo podría tomar esos mensajes y enviárselos.

En un sistema real, un prompt puede estandarizar flujos como “analizar incidente”, “resumir cliente” o “preparar revisión”. A diferencia de un tool, normalmente lo selecciona explícitamente el usuario o la aplicación.

Responsabilidades: host, cliente y servidor

  • El servidor MCP publica capacidades y ejecuta sus handlers.

  • El cliente MCP mantiene una conexión y traduce operaciones del protocolo a métodos como listTools().

  • El host es la aplicación completa (un IDE o asistente, por ejemplo). Puede contener el cliente, mostrar resources/prompts y permitir que un modelo elija tools.

Este repositorio implementa servidor y cliente, pero no un host con LLM. Por eso la demo llama sumar directamente: hace visible el mecanismo MCP sin depender de una API de IA.

Cómo extender este template

  1. Mantener createServer() independiente del transporte.

  2. Registrar cada capacidad con un nombre, una descripción y schemas claros.

  3. Mover integraciones reales a módulos de dominio; los handlers MCP deberían ser adaptadores pequeños.

  4. Añadir manejo de errores esperado con resultados que incluyan isError: true.

  5. Añadir tests del servidor con un transporte en memoria antes de introducir HTTP.

  6. Usar Streamable HTTP cuando el servidor deje de ser un proceso local y deba atender conexiones remotas; ahí también habrá que diseñar autenticación, sesiones y despliegue.

Regla importante de stdio

stdout es el canal del protocolo. Cualquier console.log() en el servidor puede corromper los mensajes JSON-RPC. Los logs del servidor deben enviarse a stderr con console.error(). El cliente sí puede imprimir normalmente su propia salida.

Referencias oficiales

Available Tools

1 tool
sumarSumar dos numerosA

Suma dos numeros y devuelve el resultado.

ParametersJSON Schema
NameRequiredDescriptionDefault
aYesPrimer sumando
bYesSegundo sumando

TDQS

A3.8/5.0
Behavior3/5

Does the description disclose side effects, auth requirements, rate limits, or destructive behavior?

No annotations are present, so the description carries the burden of behavioral disclosure. It states that the operation adds and returns a result, which implies a pure, side-effect-free computation, but it does not disclose edge cases, error behavior, or type handling. The description provides basic transparency without rich behavioral detail.

Agents need to know what a tool does to the world before calling it. Descriptions should go beyond structured annotations to explain consequences.

Conciseness5/5

Is the description appropriately sized, front-loaded, and free of redundancy?

The description is a single compact sentence that front-loads the action and states the output. Every word earns its place, with no redundancy or unnecessary detail.

Shorter descriptions cost fewer tokens and are easier for agents to parse. Every sentence should earn its place.

Completeness4/5

Given the tool's complexity, does the description cover enough for an agent to succeed on first attempt?

For a simple two-number addition tool with fully documented parameters, the description is nearly complete: it states the operation, the inputs are clear from the schema, and the result is mentioned. A more explicit return type or an example would improve completeness, but nothing essential is missing for correct invocation.

Complex tools with many parameters or behaviors need more documentation. Simple tools need less. This dimension scales expectations accordingly.

Parameters3/5

Does the description clarify parameter syntax, constraints, interactions, or defaults beyond what the schema provides?

Schema description coverage is 100%, with both parameters documented as 'Primer sumando' and 'Segundo sumando'. The description adds no parameter-specific semantics beyond what the schema already provides, so the baseline score of 3 is appropriate.

Input schemas describe structure but not intent. Descriptions should explain non-obvious parameter relationships and valid value ranges.

Purpose5/5

Does the description clearly state what the tool does and how it differs from similar tools?

The description uses a specific verb ('Suma') and resource ('dos numeros'), and explicitly states that it returns the result. With no sibling tools to differentiate from, the purpose is unambiguous and immediately actionable.

Agents choose between tools based on descriptions. A clear purpose with a specific verb and resource helps agents select the right tool.

Usage Guidelines3/5

Does the description explain when to use this tool, when not to, or what alternatives exist?

The description implies when to use the tool: whenever two numbers need to be added. However, it provides no explicit when-to-use or when-not-to-use guidance, and there are no sibling alternatives described. For a simple arithmetic tool, the implied usage is adequate but not fully explicit.

Agents often have multiple tools that could apply. Explicit usage guidance like "use X instead of Y when Z" prevents misuse.

Tool Schema Changelog

Recent tool additions, removals, and schema changes observed during successful MCP inspections.

  1. 1 tool updatev1.0.0
    • First observedsumar

TDQS

A3.6/5.0

Scored across 1 tool

Disambiguation5/5

With only one tool, there is no possibility of confusion. The name 'sumar' clearly indicates the addition operation, so an agent can easily identify its purpose.

Naming Consistency5/5

A single tool is trivially consistent in naming. While it uses a lone verb rather than a verb-noun pattern, there are no other tools to compare against, so consistency is perfect.

Tool Count1/5

The server exposes only one tool that performs a trivial arithmetic operation, which fits the description of a 'single trivial tool' and is an extreme mismatch for a tool set.

Completeness1/5

The server is named 'basics' but only offers addition. Obvious missing operations like subtraction, multiplication, and division leave the surface severely incomplete for even basic arithmetic.

Maintenance

ActivityMaintained
ResponsivenessNo issues

Related MCP Connectors

Related MCP Servers

  • A
    license
    Not graded
    quality
    D
    maintenance
    A minimal demonstration server showcasing MCP protocol capabilities including tools, resources, and prompts with basic examples like hello world functionality.
    1 npm
    MIT
  • A
    license
    Not graded
    quality
    D
    maintenance
    A minimal learning-focused MCP server that demonstrates core primitives like tools and resources through simple greeting functions. It provides a foundational example for connecting AI models to external data using both Streamable HTTP and stdio transports.
    10 npm
    MIT