FastMCP 项目使用教程

FastMCP 项目使用教程

fastmcp A TypeScript framework for building MCP servers. fastmcp 项目地址: https://gitcode.com/gh_mirrors/fas/fastmcp

1. 项目目录结构及介绍

FastMCP 是一个使用 TypeScript 编写的框架,用于构建 MCP(Model Context Protocol)服务器。以下是项目的目录结构及各部分的作用介绍:

fastmcp/
├── .github/              # 存放 GitHub 工作流程文件
├── src/                  # 源代码目录
│   ├── examples/         # 示例代码目录
│   ├── cli/              # 命令行工具相关代码
│   ├── server/           # 服务器核心代码
│   ├── utils/            # 工具类函数
│   └── index.ts          # 项目入口文件
├── .gitignore            # Git 忽略文件列表
├── LICENSE               # MIT 许可证文件
├── README.md             # 项目说明文件
├── eslint.config.js      # ESLint 配置文件
├── jsr.json              # JSON Schema Reader 配置文件
├── package.json          # 项目依赖和配置文件
├── pnpm-lock.yaml        # pnpm 锁文件
├── tsconfig.json         # TypeScript 配置文件
└── vitest.config.js      # Vitest 测试框架配置文件

2. 项目的启动文件介绍

项目的启动文件是 src/index.ts。以下是启动文件的基本内容和功能:

import { FastMCP } from './server/FastMCP';

const server = new FastMCP({
  name: 'My Server',
  version: '1.0.0',
});

// 添加工具示例
server.addTool({
  name: 'add',
  description: 'Add two numbers',
  parameters: {
    a: z.number(),
    b: z.number(),
  },
  execute: async (args) => {
    return String(args.a + args.b);
  },
});

// 启动服务器
server.start({
  transportType: 'stdio',
});

在这个文件中,我们创建了一个 FastMCP 实例,并配置了服务器的基本信息,如名称和版本。我们还添加了一个工具 add,用于执行两个数字的加法操作。最后,我们调用 server.start() 方法来启动服务器。

3. 项目的配置文件介绍

项目的配置文件主要包括 tsconfig.jsonpackage.json

tsconfig.json

tsconfig.json 文件是 TypeScript 的配置文件,它指定了 TypeScript 编译器的各种选项。以下是配置文件的一些基本设置:

{
  "compilerOptions": {
    "target": "ES6",
    "module": "commonjs",
    "strict": true,
    "esModuleInterop": true,
    "skipLibCheck": true,
    "forceConsistentCasingInFileNames": true
  },
  "include": ["src/**/*"],
  "exclude": ["node_modules", ".git"]
}

package.json

package.json 文件是 Node.js 项目的重要配置文件,它定义了项目的依赖、脚本和元数据。以下是 package.json 文件的一些重要字段:

{
  "name": "fastmcp",
  "version": "1.0.0",
  "description": "A TypeScript framework for building MCP servers.",
  "main": "src/index.ts",
  "scripts": {
    "start": "ts-node src/index.ts",
    "build": "tsc"
  },
  "dependencies": {
    // 项目的依赖库
  },
  "devDependencies": {
    // 开发依赖库
  }
}

scripts 字段中,我们定义了一些常用的脚本命令,如 start 用于启动服务器,build 用于构建项目。通过这些配置,我们可以方便地使用 npm startnpm run build 命令来执行相应的操作。

fastmcp A TypeScript framework for building MCP servers. fastmcp 项目地址: https://gitcode.com/gh_mirrors/fas/fastmcp