mcp-for-beginners 实战:使用 TypeScript 编写并运行自己的 MCP 客户端
教程文档人工智能【免费下载链接】mcp-for-beginnersThis open-source curriculum introduces the fundamentals of Model Context Protocol (MCP) through real-world, cross-language examples in .NET, Java, TypeScript, JavaScript, Rust and Python. Designed for developers, it focuses on practical techniques for building modular, scalable, and secure AI workflows from session setup to service orchestration.项目地址https://gitcode.com/GitHub_Trending/mc/mcp-for-beginners点击查看免费下载本指南以开源课程 mcp-for-beginners 中03-GettingStarted/02-client/solution/typescript解决方案为骨架完整讲解如何基于modelcontextprotocol/sdk编写一个可通过 stdio 传输协议与 MCP Server 通信的 TypeScript 客户端。你将掌握客户端与传输对象的创建、服务器能力Prompts / Resources / Tools的发现与调用、项目的构建与运行并能独立看懂客户端与服务端之间的调用关系与输出结果。文档定位一个开箱即用的客户端解决方案课程 Creating a client 的核心观点是客户端是直接与 MCP Server 通信、请求资源、工具和提示词的自定义应用或脚本。与 MCP Inspector 这类图形化交互工具不同自研客户端可以实现可编程、自动化的交互让开发者把 MCP 能力集成进自己的工作流。translations/de/03-GettingStarted/02-client/solution/typescript/README.md即本文依据的德语版解决方案说明英文原版见 typescript/README.md给出了该 TypeScript 客户端的完整运行指引其运行链路为npm install → npm run build编译并启动 Server → npm run client编译并启动 Client解决方案目录同时包含客户端代码与服务端代码两个部分服务端实为上一章 01-first-server 的同款演示服务器因此你可以在一对项目里直观看到 MCP 请求的完整闭环。解决方案结构一览先看solution/typescript目录的组织方式对照 solution/README.md 中的多语言结构说明typescript/ ├── package.json # 依赖与 npm scripts ├── package-lock.json ├── tsconfig.json # TypeScript 编译配置 └── src/ ├── index.ts # MCP Serveradd 工具、file 资源模板、review-code 提示词 └── client.ts # MCP Client连接、发现并调用上述能力两个关键配置文件package.json 声明了模块类型与脚本{ type: module, scripts: { build: tsc node ./build/index.js, client: tsc node ./build/client.js, inspector: npx modelcontextprotocol/inspector node build/index.js }, dependencies: { modelcontextprotocol/sdk: 1.26.0, type: ^2.7.3, uuid: ^14.0.0, zod: ^3.24.2 } }modelcontextprotocol/sdk是官方 TypeScript SDK锁文件中解析到的具体版本为 1.26.0见 package-lock.jsonzod用于在服务端定义参数 Schema。tsconfig.json 指定编译目标ES2022、模块方案Node16、输出目录./build源码目录./src。build与client两个脚本都会先执行tsc把src/下的 TypeScript 编译为build/下的 JavaScript再交由 Node 运行。第 0 步前置准备可选德语版文档特别注明Es wird empfohlen,uvzu installieren, aber es ist nicht zwingend erforderlich——即推荐安装uv但并非强制。uv是一个快速 Python 包管理器本示例主要依赖 Node.js 生态因此即使不安装uv也完全不影响运行。你需要确认本机具备Node.js建议 18SDK 依赖中hono/node-server即要求18.14.1npm随 Node.js 分发第 1 步安装依赖在typescript目录下执行npm install该命令依据 package.json 与package-lock.json安装全部运行时依赖SDK、zod、uuid 等与开发依赖TypeScript、types/node等。安装完成后node_modules中即包含modelcontextprotocol/sdk客户端与服务端代码所需的导入均可解析。第 2 步构建并启动服务器npm run build该脚本实际执行tsc node ./build/index.js先编译 src/index.ts再直接启动 MCP Server。服务器通过StdioServerTransport在标准输入/输出上收发 JSON-RPC 消息const transport new StdioServerTransport(); await server.connect(transport);从源码结构看这个服务器对外暴露了三种能力与上一章 01-first-server/solution/typescript/src/index.ts 完全一致工具add接收两个数字a、b返回其和的文本结果。参数 Schema 用 zod 声明server.tool(add, { a: z.number(), b: z.number() }, async ({ a, b }) ({ content: [{ type: text, text: String(a b) }] }) );动态资源模板file匹配file://{path}形式的 URI返回一段文本server.resource( file, new ResourceTemplate(file://{path}, { list: undefined }), async (uri, { path }) ({ contents: [{ uri: uri.href, text: File, ${path}! }] }) );提示词review-code接收一段代码字符串生成一条要求评审该代码的用户消息server.prompt( review-code, { code: z.string() }, ({ code }) ({ messages: [{ role: user, content: { type: text, text: Please review this code:\n\n${code} } }] }) );注意npm run build启动服务器后进程会保持运行等待来自 stdin 的客户端连接。建议另开一个终端执行下一步的客户端命令。第 3 步运行客户端npm run client该脚本实际执行tsc node ./build/client.js。客户端在编译后启动并通过 stdio 自动拉起服务器子进程完成握手。客户端源码逐段解析src/client.ts 是本次示例的核心共分四个阶段。阶段一导入并创建传输与客户端import { Client } from modelcontextprotocol/sdk/client/index.js; import { StdioClientTransport } from modelcontextprotocol/sdk/client/stdio.js; const transport new StdioClientTransport({ command: node, args: [./build/index.js] }); const client new Client( { name: example-client, version: 1.0.0 } ); await client.connect(transport);传输对象StdioClientTransport是进程间传输它通过command/args指定如何启动服务器子进程这里为node ./build/index.js并在两者之间建立 stdin/stdout 管道。客户端实例Client需要name与version用于 MCP 协议握手阶段的客户端标识。连接await client.connect(transport)完成初始化握手此后即可发起请求。这正是课程主文档 02-client/README.md 中强调的客户端可以自己拉起服务器也可以连接已运行的服务器的能力。阶段二发现并调用提示词const prompts await client.listPrompts(); const prompt await client.getPrompt({ name: review-code, arguments: { code: console.log(\hello\); } }); console.log(Prompt: , prompt.messages[0].content);先通过listPrompts()发现服务器注册的提示词再用getPrompt({ name, arguments })按名调用。这里调用的是服务端定义的review-codecode参数值会拼进服务端返回的提示词模板因此输出为Prompt: { type: text, text: Please review this code:\n\nconsole.log(hello); }阶段三列出资源与资源模板const resources await client.listResources(); for(let resource in resources.resources) { console.log(Resource: , resource); } const templates await client.listResourceTemplates(); for(let template of templates.resourceTemplates) { console.log(Resource template: , template.name); }listResources()返回服务器已声明的静态资源列表listResourceTemplates()返回可动态匹配的模板。服务端file://{path}是一个资源模板因此客户端输出Resource template: file阶段四调用工具const result await client.callTool({ name: add, arguments: { a: 1, b: 8 } }); console.log(Tool result: , result);callTool({ name, arguments })按名称与参数调用服务端工具。此处调用add传入a1、b8服务端计算后返回9因此输出Tool result: { content: [ { type: text, text: 9 } ] }预期输出与结果解读德语版文档给出了运行npm run client后应看到的完整输出Prompt: { type: text, text: Please review this code:\n\nconsole.log(hello); } Resource template: file Tool result: { content: [ { type: text, text: 9 } ] }逐行对照输出来源Prompt: { type: text, text: Please review this code:... }getPrompt(review-code, { code })的返回内容Resource template: filelistResourceTemplates()中的模板名fileTool result: { content: [ { type: text, text: 9 } ] }callTool(add, { a: 1, b: 8 })的结果1 8 9可以看到客户端输出的每一部分都能在服务端 src/index.ts 中找到对应定义——这正是验证客户端 ↔ 服务器契约一致性的最直接方式。若输出与上述不一致可优先核对工具名、参数名是否与服务端声明完全一致。交叉验证更完整的 TypeScript 客户端示例在 02-client/client_example_typescript.ts 中还提供了一个功能更完整的 TypeScript 客户端参考实现可作为本解决方案的延伸学习。它在基础连接之外还演示了通过listTools()列出服务器全部工具及其描述依次调用add、subtract、multiply、divide、help等计算器工具通过extractTextResult()从content数组中提取type: text的文本结果对listResources()/readResource()与listPrompts()/getPrompt()使用try/catch容错处理服务器未实现这些能力时优雅降级。该文件的运行方式与解决方案一致npm run build后npm run client其多语言运行说明与排错建议可参见 complete_examples.md。多语言对照TypeScript 客户端的定位课程解决方案按语言组织见 solution/README.mdTypeScript 方案与 Python、.NET、Java、Rust 方案解决同一问题。核心差异在于传输与异步模型可对照 complete_examples.md 中的对比表语言传输服务器启动异步模型关键库TypeScriptStdio自动拉起Async/AwaitNode MCP SDKPythonStdio自动拉起AsyncIOPython MCP SDK.NETStdio自动拉起Async/Await.NET MCP SDKJavaSSE/HTTP外部启动SyncWebFlux、MCP SDKRustStdio自动拉起Async/AwaitRust MCP SDK、TokioTypeScript 客户端与 Python 客户端solution/python/README.md在思路上高度相似都是创建传输 → 建立会话 → 列出并调用能力只是 API 命名不同TypeScript 为listResources/callToolPython 为list_resources/call_tool。如果你已经熟悉其中一种迁移到另一种的成本很低。常见问题排查结合 complete_examples.md 的排错清单运行本示例时可能遇到Module not foundnpm install未成功或未在typescript目录下执行请确认node_modules中存在modelcontextprotocol/sdk。连接失败/无输出npm run build启动的服务器进程可能已退出或被占用StdioClientTransport的command/args需能正确解析到build/index.js。Tool not foundcallTool中的name与服务器注册名不一致例如把add写成了Add。MCP 工具名区分大小写必须以服务器声明为准。参数校验失败add工具的a/b必须是数字服务端 zod Schemaz.number()会在类型不匹配时拒绝请求。调试建议可先用 MCP Inspector 验证服务器能力package.json中已内置npm run inspector再运行客户端便于区分问题出在服务端还是客户端。后续进阶本示例的客户端是显式调用式的——每次调用工具、资源或提示词都需要在代码中写死。在真实场景中用户更希望用自然语言与系统交互。课程的下一章 Creating a client with an LLM 会在此基础上为客户端接入大语言模型客户端先列出服务器能力并保存其 Schema再把能力以 LLM 可理解的格式注入提示从而让用户用自然语言驱动 MCP 服务器这正是从脚本化客户端走向智能体客户端的关键一步。关键要点客户端既能发现list服务器能力也能调用call服务器能力是验证服务器功能的最佳手段之一。StdioClientTransport通过command/args自动拉起服务器子进程因此本示例可以一条命令完成端到端验证客户端同样支持连接已运行的服务器。客户端输出的每一项Prompt / Resource template / Tool result都能在服务端源码中找到对应注册契约一致性是可验证、可测试的。赞分享教程文档人工智能【免费下载链接】mcp-for-beginnersThis open-source curriculum introduces the fundamentals of Model Context Protocol (MCP) through real-world, cross-language examples in .NET, Java, TypeScript, JavaScript, Rust and Python. Designed for developers, it focuses on practical techniques for building modular, scalable, and secure AI workflows from session setup to service orchestration.项目地址https://gitcode.com/GitHub_Trending/mc/mcp-for-beginners点击查看免费下载相关推荐mcp-for-beginners 实战用 TypeScript 编写并运行你的第一个 MCP 客户端mcp for beginners 实战用 TypeScript 编写并运行你的第一个 MCP 客户端 本篇文章基于开源课程 mcp for beginner教程文档人工智能mcp-for-beginners 实战用 TypeScript / Python / .NET / Java / Rust 编写你自己的 MCP 客户端mcp for beginners 实战用 TypeScript / Python / .NET / Java / Rust 编写你自己的 MCP 客户端 客教程文档人工智能mcp-for-beginners 实战用 TypeScript、Python、.NET、Java 与 Rust 编写并连接你自己的 MCP 客户端mcp for beginners 实战用 TypeScript、Python、.NET、Java 与 Rust 编写并连接你自己的 MCP 客户端 本文是教程文档人工智能创作声明:本文部分内容由AI辅助生成(AIGC),仅供参考