首页
/ MCP-GraphQL 项目使用教程

MCP-GraphQL 项目使用教程

2025-04-21 18:51:33作者:蔡怀权

1. 项目目录结构及介绍

MCP-GraphQL 项目是一个基于 Model Context Protocol (MCP) 的 GraphQL 服务器实现。以下是其目录结构及其文件/文件夹的简要介绍:

mcp-graphql/
├── .github/              # GitHub 工作流程和模板文件
├── .vscode/             # Visual Studio Code 项目配置
├── src/                 # 源代码目录
├── .gitattributes        # Git 属性配置文件
├── .gitignore           # Git 忽略文件配置
├── .npmrc               # npm 配置文件
├── CONTRIBUTING.md      # 贡献指南
├── Dockerfile           # Docker 容器构建文件
├── LICENSE              # 项目许可证文件
├── README.md            # 项目说明文件
├── biome.json           # 项目配置文件
├── bun.lockb            # bun 包管理器锁定文件
├── package.json         # npm 包配置文件
├── smithery.yaml        # Smithery 配置文件
├── tsconfig.json        # TypeScript 配置文件
  • .github/: 包含 GitHub Actions 工作流程和其他 GitHub 相关的模板文件。
  • .vscode/: 包含 Visual Studio Code 的项目配置。
  • src/: 包含项目的所有 TypeScript 源代码。
  • .gitattributes: 配置 Git 如何处理项目中的不同文件类型。
  • .gitignore: 指定 Git 应该忽略的文件和目录。
  • .npmrc: 包含 npm 的配置设置。
  • CONTRIBUTING.md: 提供项目贡献指南。
  • Dockerfile: 用于构建项目镜像的 Docker 配置文件。
  • LICENSE: 项目使用的 MIT 许可证。
  • README.md: 项目的自述文件,介绍了项目的相关信息。
  • biome.json: 项目配置文件,用于定义项目的设置。
  • bun.lockb: bun 包管理器的锁定文件,用于确保依赖的一致性。
  • package.json: 定义了项目的依赖关系、脚本和元数据。
  • smithery.yaml: Smithery 工具的配置文件。
  • tsconfig.json: TypeScript 配置文件,用于指定编译选项。

2. 项目的启动文件介绍

项目的启动文件是 src/index.ts,它负责初始化和运行 MCP-GraphQL 服务器。以下是启动文件的基本内容:

import { createServer } from 'http';
import { ApolloServer } from 'apollo-server-express';
import { schema } from './schema';

const server = new ApolloServer({ schema });

server.applyMiddleware({ app });

const httpServer = createServer(app);
httpServer.listen({ port: 4000 }, () =>
  console.log(`🚀  Server ready at http://localhost:4000${server.graphqlPath}`)
);

这段代码创建了一个 ApolloServer 实例,应用了 GraphQL schema,并通过 HTTP 服务器进行监听。

3. 项目的配置文件介绍

项目的配置文件主要包括 package.jsontsconfig.json

  • package.json: 包含项目的元数据,如名称、版本、描述和关键字,以及项目的依赖关系和脚本。以下是一些重要的配置项:
{
  "name": "mcp-graphql",
  "version": "1.0.0",
  "description": "A Model Context Protocol server for GraphQL",
  "scripts": {
    "start": "node dist/index.js"
  },
  "dependencies": {
    "apollo-server-express": "^2.19.2",
    "graphql": "^15.3.0"
  }
}
  • tsconfig.json: TypeScript 配置文件定义了项目的 TypeScript 编译选项。以下是一些基本的配置项:
{
  "compilerOptions": {
    "target": "ES6",
    "module": "commonjs",
    "strict": true,
    "esModuleInterop": true
  },
  "include": ["src/**/*"],
  "exclude": ["node_modules"]
}

这些配置确保了 TypeScript 代码被正确编译为 JavaScript,并且模块系统使用 commonjs 规范。

登录后查看全文
热门项目推荐