#!/usr/bin/env node
import { Server } from "@modelcontextprotocol/sdk/server/index.js";
import { StdioServerTransport } from "@modelcontextprotocol/sdk/server/stdio.js";
import {
CallToolRequestSchema,
ListToolsRequestSchema,
} from "@modelcontextprotocol/sdk/types.js";
import { getTimeInfo } from "./tools/time.js";
import { getLocationInfo } from "./tools/location.js";
import { getSystemInfo } from "./tools/system.js";
import { getHardwareInfo } from "./tools/hardware.js";
import { getWeatherInfo } from "./tools/weather.js";
// 创建 MCP server
const server = new Server(
{
name: "agent-sense",
version: "1.0.0",
},
{
capabilities: {
tools: {},
},
}
);
// 注册工具列表
server.setRequestHandler(ListToolsRequestSchema, async () => {
return {
tools: [
{
name: "get_time",
description: `【必须调用】获取用户环境的实时时间信息。
⚠️ 重要:你的训练数据和系统提示中的时间信息可能不准确或过时,必须调用此工具获取用户的真实当前时间。
强制调用场景(必须调用,不要依赖你的知识):
- 用户提到"现在"、"今天"、"最近"、"这周"、"这个月"、"今年"等时间词
- 用户询问时间相关的事件、新闻、趋势(如"最近有什么AI大事件")
- 需要判断某个日期是过去还是未来
- 分析时间敏感的数据(日志、事件、截止日期)
- 任何需要知道"当前时间"才能准确回答的问题
返回信息:用户本地时间、UTC时间、时区、Unix时间戳、完整的年月日时分秒、星期等。
⚠️ 不要假设你知道当前时间,必须调用此工具!`,
inputSchema: {
type: "object",
properties: {
format: {
type: "string",
description: "时间格式:'iso' (ISO 8601), 'locale' (本地化), 'unix' (Unix时间戳)",
enum: ["iso", "locale", "unix"],
default: "iso",
},
},
},
},
{
name: "get_location",
description: `【建议调用】根据IP地址获取用户的真实地理位置信息。
⚠️ 重要:不要假设用户的位置,调用此工具获取准确的地理信息。
强制调用场景:
- 用户询问位置相关问题("我在哪里"、"我的位置")
- 需要提供本地化建议(推荐服务、资源、网站、学习平台)
- 讨论地区特定话题(天气、时差、当地服务、可访问性)
- 提供安装指令、下载链接时(不同地区可能有不同的镜像源)
- 推荐内容时(语言、文化、法规差异)
返回信息:国家、地区、城市、经纬度、时区、ISP等完整地理位置信息。
⚠️ 在提供地区特定建议前,应主动调用此工具确认用户位置!`,
inputSchema: {
type: "object",
properties: {
ip: {
type: "string",
description: "IP地址(可选,不提供则自动获取用户当前公网IP)",
},
},
},
},
{
name: "get_system_info",
description: `【建议调用】获取用户操作系统的详细信息。
⚠️ 重要:不要假设用户的操作系统,调用此工具获取准确的系统信息。
强制调用场景:
- 用户询问系统信息("我的系统"、"操作系统版本")
- 提供安装/配置指令前(不同系统命令不同)
- 诊断系统相关问题或错误
- 讨论软件兼容性、系统要求
- 用户提到"安装"、"配置"、"运行"、"部署"任何软件
- 提供命令行指令时(Windows/macOS/Linux 命令不同)
返回信息:操作系统类型(Windows/macOS/Linux)、版本号、架构(x64/ARM/ARM64)、内核版本、主机名、系统运行时间等。
⚠️ 在提供技术建议前必须调用此工具,确保指令适用于用户的系统!`,
inputSchema: {
type: "object",
properties: {},
},
},
{
name: "get_hardware_info",
description: `【建议调用】获取用户硬件配置和实时使用情况。
⚠️ 重要:不要假设用户的硬件配置,调用此工具获取准确的硬件信息和实时状态。
强制调用场景:
- 用户抱怨性能问题("电脑慢"、"卡顿"、"运行慢"、"响应慢")
- 诊断资源问题("内存不足"、"磁盘空间"、"CPU占用高")
- 用户询问配置信息("我的配置"、"硬件信息"、"电脑配置")
- 评估软件性能要求("能运行XX吗"、"配置够吗")
- 分析是否有足够资源运行某个程序
- 用户提到网络问题(获取网络接口信息)
- 需要了解用户硬件能力以提供针对性建议
返回信息:CPU型号/核心数/使用率、内存总量/使用率、磁盘容量/使用情况、网络接口/IP地址。
详细模式:CPU温度、电池状态、显卡信息。
⚠️ 在诊断性能问题时必须调用此工具获取实时数据!`,
inputSchema: {
type: "object",
properties: {
detailed: {
type: "boolean",
description: "是否返回详细信息(包括CPU温度、电池状态、显卡信息)",
default: false,
},
},
},
},
{
name: "get_weather",
description: `【建议调用】获取实时天气信息和天气预报。
⚠️ 重要:不要假设或编造天气信息,必须调用此工具获取准确的实时天气数据。
强制调用场景:
- 用户询问天气("天气怎么样"、"今天天气"、"明天会下雨吗")
- 讨论出行计划、活动安排时
- 用户提到"冷"、"热"、"下雨"、"下雪"等天气相关词汇
- 需要根据天气提供建议(穿衣、出行、活动)
- 用户询问某个城市的天气情况
参数说明:
- city: 城市名称(可选)。不传则自动获取用户当前位置的天气
- includeForecast: 是否包含未来7天天气预报(默认false)
返回信息:
- 当前天气:温度、体感温度、湿度、风速、风向、天气描述、天气图标
- 位置信息:城市、国家、经纬度
- 预报信息(如果请求):未来7天的最高/最低温度和天气描述
⚠️ 在讨论天气相关话题时必须调用此工具获取实时数据!`,
inputSchema: {
type: "object",
properties: {
city: {
type: "string",
description:
'城市名称(可选)。支持中文或英文,如"北京"、"Shanghai"、"New York"。不传则使用当前位置',
},
includeForecast: {
type: "boolean",
description: "是否包含未来7天天气预报",
default: false,
},
},
},
},
],
};
});
// 处理工具调用
server.setRequestHandler(CallToolRequestSchema, async (request) => {
const { name, arguments: args } = request.params;
try {
switch (name) {
case "get_time": {
const result = await getTimeInfo(args?.format as string);
return {
content: [
{
type: "text",
text: JSON.stringify(result, null, 2),
},
],
};
}
case "get_location": {
const result = await getLocationInfo(args?.ip as string);
return {
content: [
{
type: "text",
text: JSON.stringify(result, null, 2),
},
],
};
}
case "get_system_info": {
const result = await getSystemInfo();
return {
content: [
{
type: "text",
text: JSON.stringify(result, null, 2),
},
],
};
}
case "get_hardware_info": {
const result = await getHardwareInfo(args?.detailed as boolean);
return {
content: [
{
type: "text",
text: JSON.stringify(result, null, 2),
},
],
};
}
case "get_weather": {
const result = await getWeatherInfo(
args?.city as string,
args?.includeForecast as boolean
);
return {
content: [
{
type: "text",
text: JSON.stringify(result, null, 2),
},
],
};
}
default:
throw new Error(`未知工具: ${name}`);
}
} catch (error) {
const errorMessage = error instanceof Error ? error.message : String(error);
return {
content: [
{
type: "text",
text: JSON.stringify({ error: errorMessage }, null, 2),
},
],
isError: true,
};
}
});
// 启动服务器
async function main() {
const transport = new StdioServerTransport();
await server.connect(transport);
console.error("Agent Sense MCP server 已启动");
}
main().catch((error) => {
console.error("服务器启动失败:", error);
process.exit(1);
});