模型上下文协议示例

提供使用TypeScript实现模型上下文协议(MCP)的示例代码,包括服务器和客户端的实现,支持密码生成和图像验证等功能。

  • by rstropek
  • TypeScript
  • 本地部署
  • 1 星标
  • 更新于
在 GitHub 查看 rstropek/2025-10-15-mcp

使用说明

Introduction to Model Context Protocol (MCP)

Overview

This repository contains samples for an introduction to the Model Context Protocol (MCP) using TypeScript.

Before you can get started with these samples, install the dependencies with npm install. Next, compile the samples with npm run build. You can start the diffeent samples with the npm run start:<sample-name> commands (see the scripts section in package.json for all available samples).

Samples

Sample 1: MCP Server Without SDK

This sample demonstrates how to set up an MCP server without using the MCP SDK. It communicates with the MCP client using raw jsonRPC messages. Do not write MCP server like this in production! This is just for educational purposes to show how the protocol works under the hood.

The MCP server can generate passwords by concatinating character names from the TV show My Little Pony.

Sample 2: MCP Server With SDK

The second sample implements the same functionality as the first sample, but this time it uses the MCP SDK. This makes the implementation much simpler and more robust.

The sample contains tools, a prompt, and a resource.

Sample 3: MCP Server With Sampling

This example introduces the concept of sampling in MCP. The server can generate passwords by sampling characters from My Little Pony.

Sample 4: MCP Server With Sampling and Image Processing

This example shows how to work with content that is not text. It implements an MCP server that uses sampling to verify images. If you want to try this MCP server, perform the following steps:

  1. Run the sample web server with npm run start:server.

  2. Enable the Verify Image tool in the MCP configuration.

  3. Try the following prompt:

    Use the playwright mcp server to open http://localhost:3000/ and create a screenshot. Then use the verify-image MCP server to check if the screenshot claims that C# is "awesome".
    

Sample 5: Simple MCP Client

This sample shows how to create an MCP client with stdio transport. It queries the server for the list of tools.