当前位置: 首页 > news >正文

MCP-代码解读TypeScript版本

MCP-代码解读TypeScript版本

文章目录

  • MCP-代码解读TypeScript版本
    • 1-参考网址
    • 2-TypeScript代码
    • 3-代码解读
      • 1-[非重点]定义函数
      • 2-[非重点]定义工具说明
      • 3-[重点]运行MCP服务

1-参考网址

  • B站视频参考

2-TypeScript代码

import { McpServer } from "@modelcontextprotocol/sdk/server/mcp.js";
import { StdioServerTransport } from "@modelcontextprotocol/sdk/server/stdio.js";
import { z } from "zod";// 定义天气数据类型
interface WeatherInfo {temperature: number;condition: string;humidity: number;windSpeed: number;
}// 模拟城市天气数据
const weatherData = {"北京": { temperature: 20, condition: "晴朗", humidity: 45, windSpeed: 8 },"上海": { temperature: 25, condition: "多云", humidity: 60, windSpeed: 12 },"广州": { temperature: 28, condition: "小雨", humidity: 75, windSpeed: 6 },"深圳": { temperature: 27, condition: "阴天", humidity: 70, windSpeed: 10 },"杭州": { temperature: 22, condition: "多云", humidity: 65, windSpeed: 9 }
} as const satisfies Record<string, WeatherInfo>;// 模拟函数调用和数据返回
function getWeatherInfo(city: string): { content: Array<{ type: "text"; text: string }> } {const weather = weatherData[city as keyof typeof weatherData];if (!weather) {const availableCities = Object.keys(weatherData).join("、");return {content: [{type: "text",text: `未找到城市 ${city} 的天气信息。支持的城市包括:${availableCities}`}]};}// 模板字符串格式const weatherText = `${city}的天气信息:温度:${weather.temperature}°C天气:${weather.condition}湿度:${weather.humidity}%风速:${weather.windSpeed}m/s`.replace(/^\s+/gm, "");  // 使用正则移除行首空格return {content: [{type: "text",text: weatherText}]};
}// 创建 MCP 服务器
const server = new McpServer({name: "weather-server",version: "1.0.0",description: "城市天气信息服务"
});// 注册天气查询工具
server.tool("get-weather","获取指定城市的天气信息",{city: z.string().describe("城市名称(如:北京、上海、广州、深圳)")},async ({ city }) => {return getWeatherInfo(city);}
);// 启动服务器
async function main() {const transport = new StdioServerTransport();await server.connect(transport);
}main().catch((error) => {console.error("初始化失败:", error);process.exit(1);
});

3-代码解读

1-[非重点]定义函数

这个地方填写你的业务逻辑:函数/API/数据库等业务逻辑

// 定义天气数据类型
interface WeatherInfo {temperature: number;condition: string;humidity: number;windSpeed: number;
}// 模拟城市天气数据
const weatherData = {"北京": { temperature: 20, condition: "晴朗", humidity: 45, windSpeed: 8 },"上海": { temperature: 25, condition: "多云", humidity: 60, windSpeed: 12 },"广州": { temperature: 28, condition: "小雨", humidity: 75, windSpeed: 6 },"深圳": { temperature: 27, condition: "阴天", humidity: 70, windSpeed: 10 },"杭州": { temperature: 22, condition: "多云", humidity: 65, windSpeed: 9 }
} as const satisfies Record<string, WeatherInfo>;// 模拟函数调用和数据返回
function getWeatherInfo(city: string): { content: Array<{ type: "text"; text: string }> } {const weather = weatherData[city as keyof typeof weatherData];if (!weather) {const availableCities = Object.keys(weatherData).join("、");return {content: [{type: "text",text: `未找到城市 ${city} 的天气信息。支持的城市包括:${availableCities}`}]};}// 模板字符串格式const weatherText = `${city}的天气信息:温度:${weather.temperature}°C天气:${weather.condition}湿度:${weather.humidity}%风速:${weather.windSpeed}m/s`.replace(/^\s+/gm, "");  // 使用正则移除行首空格return {content: [{type: "text",text: weatherText}]};
}

2-[非重点]定义工具说明

这个过程有点类似FunctionCall的【函数描述】+【JsonSchema】定义的过程

import { McpServer } from "@modelcontextprotocol/sdk/server/mcp.js";// 创建 MCP 服务器
const server = new McpServer({name: "weather-server",version: "1.0.0",description: "城市天气信息服务"
});// 注册天气查询工具
server.tool("get-weather","获取指定城市的天气信息",{city: z.string().describe("城市名称(如:北京、上海、广州、深圳)")},async ({ city }) => {return getWeatherInfo(city);}
);

3-[重点]运行MCP服务

import { StdioServerTransport } from "@modelcontextprotocol/sdk/server/stdio.js";// 启动服务器
async function main() {const transport = new StdioServerTransport();await server.connect(transport);console.error("天气服务器已启动");
}main().catch((error) => {console.error("服务器启动失败:", error);process.exit(1);
});

http://www.mrgr.cn/news/93868.html

相关文章:

  • 尚硅谷TS快速入门笔记(个人笔记用)
  • 通义万相2.1技术深度解析
  • 【面试】框架
  • 6-langchang多模态输入和自定义输出
  • 【Java学习】泛型
  • 【面试】MySQL
  • 力扣刷题(数组篇)
  • Dify 本地部署问题:install 界面一直转圈
  • 计算机网络----主要内容简介
  • Dify 本地部署教程
  • 《今日AI-人工智能-编程日报》
  • 对开源VLA sota π0的微调——如何基于各种开源数据集、以及你自己的私有数据集微调π0(含我司的微调实践)
  • 群晖DS 223 Docker:开启私有云
  • 配置 Thunderbird 以使用 QQ 邮箱
  • 【五.LangChain技术与应用】【9.LangChain ChatPromptTemplate(上):高级对话模板设计】
  • Android Native 之 文件系统挂载
  • 读书会-c#并发编程
  • vue3 使用sass变量
  • vue3中使用h()函数加载elementPlus 组件
  • C语言数据结构之顺序表