惯性聚合 高效追踪和阅读你感兴趣的博客、新闻、科技资讯
阅读原文 在惯性聚合中打开

推荐订阅源

Hacker News: Ask HN
Hacker News: Ask HN
H
Help Net Security
Microsoft Azure Blog
Microsoft Azure Blog
B
Blog RSS Feed
Jina AI
Jina AI
Stack Overflow Blog
Stack Overflow Blog
量子位
博客园_首页
Vercel News
Vercel News
CTFtime.org: upcoming CTF events
CTFtime.org: upcoming CTF events
Forbes - Security
Forbes - Security
IT之家
IT之家
N
News and Events Feed by Topic
S
Security Affairs
Recent Commits to openclaw:main
Recent Commits to openclaw:main
Webroot Blog
Webroot Blog
Recorded Future
Recorded Future
L
LangChain Blog
Y
Y Combinator Blog
AI
AI
MyScale Blog
MyScale Blog
大猫的无限游戏
大猫的无限游戏
小众软件
小众软件
Know Your Adversary
Know Your Adversary
AWS News Blog
AWS News Blog
Help Net Security
Help Net Security
Cyberwarzone
Cyberwarzone
L
Lohrmann on Cybersecurity
OSCHINA 社区最新新闻
OSCHINA 社区最新新闻
Google Online Security Blog
Google Online Security Blog
V2EX - 技术
V2EX - 技术
奇客Solidot–传递最新科技情报
奇客Solidot–传递最新科技情报
PCI Perspectives
PCI Perspectives
I
Intezer
T
Tenable Blog
G
Google Developers Blog
Application and Cybersecurity Blog
Application and Cybersecurity Blog
T
Troy Hunt's Blog
L
LINUX DO - 最新话题
云风的 BLOG
云风的 BLOG
C
CXSECURITY Database RSS Feed - CXSecurity.com
有赞技术团队
有赞技术团队
O
OpenAI News
P
Proofpoint News Feed
TaoSecurity Blog
TaoSecurity Blog
C
Check Point Blog
Last Week in AI
Last Week in AI
S
Schneier on Security
Simon Willison's Weblog
Simon Willison's Weblog
Blog — PlanetScale
Blog — PlanetScale

李宇博客

web图像色彩 相册 - 夏天 | 李宇Blog Iphone SE2 体验 IOS14 体验 相册 - 三月 | 李宇Blog “视频” 是怎么来的? 相册 - 上海双年展 相册 - 西安 | 李宇Blog 过去几年用过的一些手机 世界杯 Dusha字体 一年的时间 相册 - 朱家角 | 李宇Blog
MCP 怎么用
李宇 · 2025-11-21 · via 李宇博客
import { readdir, stat } from 'fs/promises'
import { join } from 'path'
import { homedir } from 'os'
import { McpServer } from '@modelcontextprotocol/sdk/server/mcp.js'
import { StdioServerTransport } from '@modelcontextprotocol/sdk/server/stdio.js'

// 创建 MCP 服务器实例
// name 和 version 用于标识服务器
const server = new McpServer({
  name: 'desktop_folders',
  version: '1.0.0',
})

/**
 * 注册工具:list_desktop_folders
 *
 * server.tool() 方法用于注册一个可被 AI 调用的工具
 * 参数说明:
 *   - 第一个参数:工具名称(AI 通过这个名称调用工具)
 *   - 第二个参数:工具的参数定义(这里为空对象,表示不需要参数)
 *   - 第三个参数:工具的执行函数(异步函数,返回工具执行结果)
 */
server.tool(
  'list_desktop_folders',
  {}, // 工具参数定义(空对象表示无参数)
  async () => {
    try {
      // 获取桌面路径(跨平台兼容)
      const desktopPath = join(homedir(), 'Desktop')

      // 读取桌面目录下的所有文件和文件夹
      const items = await readdir(desktopPath)

      // 并行检查每个项目是否为文件夹(提高性能)
      const folderChecks = await Promise.all(
        items.map(async (item) => {
          const itemPath = join(desktopPath, item)
          const stats = await stat(itemPath)
          return stats.isDirectory() ? item : null
        })
      )

      // 过滤出文件夹名称(移除 null 值)
      const folders = folderChecks.filter(Boolean)

      // 格式化返回结果
      // MCP 工具必须返回特定格式:content 数组包含文本内容
      return {
        content: [
          {
            type: 'text',
            text:
              folders.length > 0
                ? `桌面上的文件夹:\n${folders.join('\n')}\n\n共 ${folders.length} 个文件夹`
                : '桌面上没有文件夹',
          },
        ],
      }
    } catch (error) {
      // 错误处理:返回错误信息并标记为错误
      return {
        content: [
          {
            type: 'text',
            text: `错误:${error.message}`,
          },
        ],
        isError: true,
      }
    }
  }
)

/**
 * 启动服务器
 *
 * StdioServerTransport 使用标准输入输出进行通信
 * 这是 MCP 服务器最常见的通信方式
 */
const transport = new StdioServerTransport()
await server.connect(transport)

import { readdir, stat } from 'fs/promises'
import { join } from 'path'
import { homedir } from 'os'
import { McpServer } from '@modelcontextprotocol/sdk/server/mcp.js'
import { StdioServerTransport } from '@modelcontextprotocol/sdk/server/stdio.js'

// 创建 MCP 服务器实例
// name 和 version 用于标识服务器
const server = new McpServer({
  name: 'desktop_folders',
  version: '1.0.0',
})

/**
 * 注册工具:list_desktop_folders
 *
 * server.tool() 方法用于注册一个可被 AI 调用的工具
 * 参数说明:
 *   - 第一个参数:工具名称(AI 通过这个名称调用工具)
 *   - 第二个参数:工具的参数定义(这里为空对象,表示不需要参数)
 *   - 第三个参数:工具的执行函数(异步函数,返回工具执行结果)
 */
server.tool(
  'list_desktop_folders',
  {}, // 工具参数定义(空对象表示无参数)
  async () => {
    try {
      // 获取桌面路径(跨平台兼容)
      const desktopPath = join(homedir(), 'Desktop')

      // 读取桌面目录下的所有文件和文件夹
      const items = await readdir(desktopPath)

      // 并行检查每个项目是否为文件夹(提高性能)
      const folderChecks = await Promise.all(
        items.map(async (item) => {
          const itemPath = join(desktopPath, item)
          const stats = await stat(itemPath)
          return stats.isDirectory() ? item : null
        })
      )

      // 过滤出文件夹名称(移除 null 值)
      const folders = folderChecks.filter(Boolean)

      // 格式化返回结果
      // MCP 工具必须返回特定格式:content 数组包含文本内容
      return {
        content: [
          {
            type: 'text',
            text:
              folders.length > 0
                ? `桌面上的文件夹:\n${folders.join('\n')}\n\n共 ${folders.length} 个文件夹`
                : '桌面上没有文件夹',
          },
        ],
      }
    } catch (error) {
      // 错误处理:返回错误信息并标记为错误
      return {
        content: [
          {
            type: 'text',
            text: `错误:${error.message}`,
          },
        ],
        isError: true,
      }
    }
  }
)

/**
 * 启动服务器
 *
 * StdioServerTransport 使用标准输入输出进行通信
 * 这是 MCP 服务器最常见的通信方式
 */
const transport = new StdioServerTransport()
await server.connect(transport)