模型上下文协议服务器

一个强大的模型上下文协议(MCP)服务器,动态提供项目上下文,作为LLM代理与项目API和数据库交互的桥梁。

  • by WilliamPinto-Olmos
  • TypeScript
  • 本地部署
  • 更新于
在 GitHub 查看 WilliamPinto-Olmos/project-mcp-server

使用说明

Project MCP服务器

一个强大的 模型上下文协议(MCP) 动态提供项目上下文的服务器。它充当桥梁,允许LLM代理探索并与项目的API(通过OpenAPI)和数据库交互。

快速启动(零配置)

使用此服务器的最简单方法是直接使用 npx。这不需要对项目进行代码更改。

使用npx运行

对于Cursor/Claude桌面: 将此添加到MCP设置配置中:

{
  "mcpServers": {
    "my-project": {
      "command": "npx",
      "args": ["-y", "@williamp29/project-mcp-server"],
      "env": {
        "PROJECT_MCP_API_BASE_URL": "...",
        "PROJECT_MCP_DB_HOST": "..."
      }
    }
  }
}

(注意:如果您的MCP客户端支持,您可以直接在JSON配置中传递环境变量,也可以从文件中加载它们。)


图书馆集成(高级)

为了实现深度集成,请将该包作为依赖项安装在Node.js项目中。这允许您自定义身份验证、添加自定义工具或以编程方式使用它。

1.安装

npm install @williamp29/project-mcp-server

2.创建入口点

创建文件(例如。, mcp-server.ts)要配置和启动服务器,请执行以下操作:

import { MCPServer, MySQLDriver } from "@williamp29/project-mcp-server";
import { AuthStrategy, GlobalAuthContext } from "@williamp29/project-mcp-server/api-explorer";

// 1. (Optional) Define a custom authentication strategy for APIs
class MyCustomAuth implements AuthStrategy {
  name = "MyCustomAuth";
  async getHeaders() {
    return { "Authorization": `Bearer ${process.env.MY_API_TOKEN}` };
  }
}

// 2. (Optional) Initialize a database driver
// You can use the built-in MySQLDriver
const mysqlDriver = new MySQLDriver({
  host: process.env.DB_HOST || "localhost",
  port: 3306,
  user: "root",
  password: "password",
  database: "my_database"
});

// 3. Create and start the server
const server = new MCPServer({
  specPath: "./openapi-spec.json", // Optional: Omit to skip API explorer
  api: {
    baseUrl: "https://api.example.com",
    authContext: new GlobalAuthContext(new MyCustomAuth()),
  },
  database: { // Optional: Pass undefined to skip database initialization
    driver: mysqlDriver,
    permissions: {
      enableRunQuery: true,
      enableRunUpdateStatement: true,
      enableRunDeleteStatement: false,
      enableRunStatement: false
    }
  }
});

await server.start();

生命周期管理

这 MCPServer 类提供了以编程方式管理服务器生命周期的方法:

方法 说明
start() 初始化API资源管理器和数据库连接,然后连接到传输。
stop() 优雅地断开与数据库的连接并关闭MCP服务器。
reload() 重新启动服务器,清除已注册的工具并重新初始化所有配置。

例子:

await server.start();

// Later, to restart the server with refreshed configuration:
await server.reload();

// To shut down the server:
await server.stop();

3.自定义数据库驱动程序

您可以通过满足以下条件来实现自己的数据库驱动程序 DbDriver 界面。这允许您在MCP服务器上使用任何数据库(PostgreSQL、SQLite等)。

import { MCPServer, DbDriver, DbDriverPermissions, DbDriverToolDefinitions } from "@williamp29/project-mcp-server";

class MyPostgresDriver implements DbDriver {
  name = "postgres";
  
  async connect() { /* ... */ }
  async disconnect() { /* ... */ }
  
  // Implement introspection methods
  async listTables() { /* ... */ }
  async listTableNames() { return ["table1", "table2"]; }
  async describeTable(table: string) { /* ... */ }
  // ... other DbDriver methods

  // Define which tools this driver supports
  getToolDefinitions(permissions: DbDriverPermissions): DbDriverToolDefinitions {
    return {
      db_list_tables: { description: "List all tables" },
      // ...
    };
  }

  // Handle tool execution
  async handleToolCall(name: string, args: any) {
    if (name === "db_list_tables") return this.listTables();
    // ...
  }
}

const server = new MCPServer({
  specPath: "./openapi-spec.json",
  api: {
    baseUrl: "https://api.example.com",
  },
  database: {
    driver: new MyPostgresDriver()
  }
});

3.在package.json中添加脚本

{
  "scripts": {
    "mcp": "ts-node mcp-server.ts"
  }
}

4.在游标中使用

{
  "mcpServers": {
    "my-integrated-project": {
      "command": "npm",
      "args": ["run", "mcp"],
      "cwd": "/absolute/path/to/your/project"
    }
  }
}

配置参考

环境变量

这些变量控制服务器行为。如果您使用 npx 如果你使用 dotenv 在您的自定义脚本中。

变量 描述 默认值
PROJECT_MCP_API_BASE_URL 目标API的基本URL -
PROJECT_MCP_AUTH_TYPE 认证方法: bearer, identity, none none
PROJECT_MCP_AUTH_IDENTIFIER 的默认ID identity auth -
PROJECT_MCP_DB_HOST 数据库主机名 -
PROJECT_MCP_DB_PORT 数据库端口 3306
PROJECT_MCP_DB_USER 数据库用户名 -
PROJECT_MCP_DB_PASSWORD 数据库密码 -
PROJECT_MCP_DB_DATABASE 数据库名称 -

特性

  • API资源管理器:

    • 自动解析OpenAPI 3.0规范。
    • 展示如下工具 api_get_tags, api_get_endpoints,以及 api_call_endpoint.
    • 支持动态路径参数和JSON正文。
    • api_set_identity:在会话期间动态切换API调用的活动用户上下文。
  • 数据库浏览器:

    • 检查架构 db_list_tables, db_list_table_names, db_describe_tables,以及 db_get_schemas.
    • 通过以下方式发现数据库结构 db_get_relationships (支持表过滤)。
    • 分析数据 db_get_table_stats 和 db_sample_rows.
    • 使用以下命令运行经过验证的SQL db_run_query, db_run_update_statement,以及 db_run_delete_statement.
    • 可扩展架构:支持 自定义驱动程序!
    • 包含 MySQLDriver 直接使用MySQL/MariaDB。