MCP cover image
See in Github
2025-03-25

Un exemple MCP simple avec le client et le serveur

1

Github Watches

0

Github Forks

0

Github Stars

A Simple MCP Server and Client

This is a simple Model Context Protocol (MCP) server and client implementation that demonstrates how to expose and access weather information.

Server

The server is implemented in server.js. It provides the following capabilities:

  • Resources:

    • city://NewYork: Basic information about New York City
    • city://London: Basic information about London
    • city://Tokyo: Basic information about Tokyo
  • Tools:

    • get-weather: Get weather forecast for a city with parameters for city name and number of days
  • Prompts:

    • weather-advice: Get travel advice based on weather conditions

Client

The client is implemented in client.js. It demonstrates how to:

  • Connect to the MCP server
  • List available resources
  • Read a specific city resource
  • Call the weather forecast tool
  • Get a weather advice prompt

JSON-RPC Communication

This implementation uses JSON-RPC 2.0 as the underlying communication protocol between client and server. Key JSON-RPC concepts used:

  • Methods: Standard method names like resources/list, resources/read, tools/call, and prompts/get
  • Parameters: Input data sent with method calls
  • Request IDs: Unique identifiers to match requests with responses
  • Results/Errors: Structured response data or error information

The MCP SDK abstracts much of this, but understanding the protocol is helpful for debugging and customization. You can see the raw JSON-RPC messages by examining the console output.

Running the Server and Client

  1. Start the client:

    node client.js
    

    Starting the client will automatically start the server as a child process. The client will connect to the server and perform all the example operations.

  2. To start the server independently:

    node server.js
    

Transport Mechanism

This example uses stdio (standard input/output) as the transport mechanism between client and server. When the client is started, it spawns the server as a child process and communicates with it through stdin/stdout streams.

A custom logging transport is implemented to show the messages exchanged between the client and server. These messages are logged to the console in a human-readable format, which is helpful for understanding the protocol.

Troubleshooting

If you encounter issues with resource or prompt requests timing out, check the server implementation. The example includes direct message handling for certain request types to work around limitations in the current SDK version.

相关推荐

  • NiKole Maxwell
  • I craft unique cereal names, stories, and ridiculously cute Cereal Baby images.

  • https://suefel.com
  • Latest advice and best practices for custom GPT development.

  • Yusuf Emre Yeşilyurt
  • I find academic articles and books for research and literature reviews.

  • https://maiplestudio.com
  • Find Exhibitors, Speakers and more

  • Carlos Ferrin
  • Encuentra películas y series en plataformas de streaming.

  • Joshua Armstrong
  • Confidential guide on numerology and astrology, based of GG33 Public information

  • Emmet Halm
  • Converts Figma frames into front-end code for various mobile frameworks.

  • Elijah Ng Shi Yi
  • Advanced software engineer GPT that excels through nailing the basics.

  • lumpenspace
  • Take an adjectivised noun, and create images making it progressively more adjective!

  • apappascs
  • Découvrez la collection la plus complète et la plus à jour de serveurs MCP sur le marché. Ce référentiel sert de centre centralisé, offrant un vaste catalogue de serveurs MCP open-source et propriétaires, avec des fonctionnalités, des liens de documentation et des contributeurs.

  • ShrimpingIt
  • Manipulation basée sur Micropython I2C de l'exposition GPIO de la série MCP, dérivée d'Adafruit_MCP230XX

  • huahuayu
  • Une passerelle API unifiée pour intégrer plusieurs API d'explorateur de blockchain de type étherscan avec la prise en charge du protocole de contexte modèle (MCP) pour les assistants d'IA.

  • modelcontextprotocol
  • Serveurs de protocole de contexte modèle

  • Mintplex-Labs
  • L'application tout-en-un desktop et Docker AI avec chiffon intégré, agents AI, constructeur d'agent sans code, compatibilité MCP, etc.

    Reviews

    5 (1)
    Avatar
    user_xDO1bnw5
    2025-04-16

    As an avid user of mcp-client-server by Logesh Kumar, I must say it's an outstanding tool for efficient client-server communication. The well-documented repository on GitHub makes the setup process seamless. Its reliability and performance are exceptional, making it perfect for my projects. Highly recommend checking it out!