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

推荐订阅源

GbyAI
GbyAI
Blog — PlanetScale
Blog — PlanetScale
The GitHub Blog
The GitHub Blog
Microsoft Security Blog
Microsoft Security Blog
I
InfoQ
A
About on SuperTechFans
T
The Blog of Author Tim Ferriss
D
DataBreaches.Net
L
LangChain Blog
F
Fortinet All Blogs
C
Check Point Blog
Google DeepMind News
Google DeepMind News
云风的 BLOG
云风的 BLOG
Engineering at Meta
Engineering at Meta
Cyber Security Advisories - MS-ISAC
Cyber Security Advisories - MS-ISAC
H
Help Net Security
J
Java Code Geeks
月光博客
月光博客
H
Hackread – Cybersecurity News, Data Breaches, AI and More
IT之家
IT之家
aimingoo的专栏
aimingoo的专栏
小众软件
小众软件
宝玉的分享
宝玉的分享
Jina AI
Jina AI

李宇博客

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)