Skip to content

MCP 配置

模型上下文协议 (MCP) 允许 Sidian 连接到外部工具和服务,从而将 AI 的能力扩展到代码编辑之外。本指南将介绍如何配置和管理 MCP 服务器。

什么是 MCP?

MCP 是一种标准化协议,允许 AI 助手安全地连接到外部工具和数据源:

功能

  • 文件系统操作
  • Git 仓库管理
  • 数据库查询
  • API 集成
  • 内存和上下文存储

优点

  • 安全的沙盒化执行
  • 标准化协议
  • 可扩展的架构
  • 社区驱动的服务器
  • 简易配置

配置位置

Sidian 从以下位置读取 MCP 配置:

配置文件

  • Windows: C:\Users\{YourUsername}\.sidian\mcp.json
  • macOS: ~/.sidian/mcp.json
  • Linux: ~/.sidian/mcp.json

基本配置

MCP 配置文件使用 JSON 格式,结构如下:

json
{
  "mcpServers": {
    "filesystem": {
      "command": "npx",
      "args": ["-y", "@modelcontextprotocol/server-filesystem", "/path/to/your/workspace"],
      "env": {}
    },
    "git": {
      "command": "npx",
      "args": ["-y", "@modelcontextprotocol/server-git", "--repository", "/path/to/your/git/repository"],
      "env": {}
    },
    "memory": {
      "command": "npx",
      "args": ["-y", "@modelcontextprotocol/server-memory"],
      "env": {}
    }
  }
}

流行的 MCP 服务器

以下是一些常用的 MCP 服务器及其配置:

文件系统服务器 (Filesystem Server)

提供文件系统访问权限,用于读取、写入和管理文件:

json
{
  "mcpServers": {
    "filesystem": {
      "command": "npx",
      "args": ["-y", "@modelcontextprotocol/server-filesystem", "/path/to/your/workspace"],
      "env": {}
    }
  }
}

Git 服务器 (Git Server)

启用 Git 操作和仓库管理:

json
{
  "mcpServers": {
    "git": {
      "command": "npx",
      "args": ["-y", "@modelcontextprotocol/server-git", "--repository", "/path/to/your/git/repo"],
      "env": {}
    }
  }
}

内存服务器 (Memory Server)

为 AI 对话提供持久内存:

json
{
  "mcpServers": {
    "memory": {
      "command": "npx",
      "args": ["-y", "@modelcontextprotocol/server-memory"],
      "env": {}
    }
  }
}

数据库服务器 (Database Server)

连接到数据库以进行查询和操作:

json
{
  "mcpServers": {
    "brave-search": {
      "command": "npx",
      "args": ["-y", "@modelcontextprotocol/server-brave-search"],
      "env": {
        "BRAVE_API_KEY": "your-brave-api-key-here"
      }
    }
  }
}

高级配置

环境变量

为 MCP 服务器设置环境变量:

json
{
  "mcpServers": {
    "custom-server": {
      "command": "node",
      "args": ["/path/to/custom-server.js"],
      "env": {
        "API_KEY": "your-api-key",
        "DEBUG": "true",
        "TIMEOUT": "30000"
      }
    }
  }
}```

### 服务器选项
配置服务器特定的选项:

```json
{
  "mcpServers": {
    "web-server": {
      "command": "npx",
      "args": ["-y", "@modelcontextprotocol/server-web"],
      "options": {
        "timeout": 30000,
        "retries": 3,
        "logLevel": "info"
      }
    }
  }
}

条件配置

根据环境使用不同的配置:

json
{
  "mcpServers": {
    "filesystem": {
      "command": "npx",
      "args": [
        "-y",
        "@modelcontextprotocol/server-filesystem",
        "        "${SIDIAN_PROJECT_ROOT:-/default/path}""
      ]
    }
  }
}```

## 安全注意事项

### 沙盒化 (Sandboxing)
- MCP 服务器在隔离的进程中运行
- 对系统资源的访问受限
- 可配置的权限边界
- 对所有操作进行审计日志记录

### 访问控制
- 白名单允许的目录和文件
- 限制网络访问
- 限制命令执行
- 监控资源使用情况

### 最佳实践
- 使用最低要求的权限
- 定期更新 MCP 服务器包
- 监控服务器日志以发现可疑活动
- 对敏感数据使用环境变量

## 故障排除

### 服务器无法启动
- 检查 Node.js 是否已安装且可访问
- 验证服务器包是否已安装:`npm list -g`
- 检查 MCP 配置文件的语法
- 查看服务器日志以获取具体的错误信息
- 尝试在终端中手动运行服务器命令

### 权限错误
- 确保 Sidian 对配置的目录具有读/写权限
- 检查项目文件夹的文件系统权限
- 如有必要,以管理员身份运行 Sidian (Windows)
- 验证 MCP 配置文件是否可读

### 服务器持续崩溃
- 将 MCP 服务器包更新到最新版本
- 检查是否存在冲突的 Node.js 版本
- 增加配置中的超时值
- 监控系统资源(CPU、内存)
- 检查服务器日志中的错误模式

### 性能问题
- 减少活动的 MCP 服务器数量
- 优化服务器配置
- 使用本地服务器代替远程服务器
- 监控远程服务器的网络延迟
- 考虑缓存策略

## 自定义 MCP 服务器

### 创建自定义服务器
您可以根据特定需求创建自定义的 MCP 服务器:

```javascript
// custom-server.js
const { Server } = require('@modelcontextprotocol/sdk/server/index.js');
const { StdioServerTransport } = require('@modelcontextprotocol/sdk/server/stdio.js');

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

// 在此处添加您的自定义工具和资源
server.setRequestHandler('tools/list', async () => {
  return {
    tools: [
      {
        name: 'custom-tool',
        description: '一个用于特定操作的自定义工具',
        inputSchema: {
          type: 'object',
          properties: {
            input: { type: 'string' }
          }
        }
      }
    ]
  };
});

const transport = new StdioServerTransport();
server.connect(transport);

自定义服务器的配置

json
{
  "mcpServers": {
    "custom": {
      "command": "node",
      "args": ["/path/to/custom-server.js"]
    }
  }
}

监控和日志记录

服务器状态

在 Sidian 中监控 MCP 服务器状态:

  • 在设置中检查服务器连接状态
  • 在输出面板中查看服务器日志
  • 监控资源使用情况
  • 跟踪错误率和性能

调试

启用调试日志以进行故障排除:

json
{
  "mcpServers": {
    "debug-server": {
      "command": "npx",
      "args": ["-y", "@modelcontextprotocol/server-filesystem", "/path"],
      "env": {
        "DEBUG": "*",
        "LOG_LEVEL": "debug"
      }
    }
  }
}

MCP 配置使您可以显著扩展 Sidian 的 AI 功能。从基础服务器开始,并根据需要逐步添加更复杂的配置。