Skip to main content
Glama
RianHarahap

my-mcp-server

by RianHarahap

My MCP Server(面向初学者)

自制 MCP(Model Context Protocol)服务器,包含:

  • 通过 .env 文件配置自己的 API key 和 URL

  • 示例工具:echo 和 call_my_api

  • 可直接用 MCP Inspector 测试

  • 可直接在 VS Code(GitHub Copilot Chat)中使用

1. 安装依赖

npm install

Related MCP server: mcp-server-demo

2. 创建 .env 文件

将 .env.example 复制为 .env,然后填入你自己的 API key 和 URL:

Copy-Item .env.example .env

.env 文件内容:

MY_API_KEY=api-key-asli-kamu
MY_API_BASE_URL=https://api.contohkamu.com
MCP_SERVER_NAME=my-mcp-server

⚠️ 切勿将 .env 文件提交到 git。该文件已通过 .gitignore 自动忽略。

3. 构建项目

npm run build

此命令会将 src/ 中的 TypeScript 编译为 build/ 中的 JavaScript。

4. 使用 MCP Inspector 测试

MCP Inspector 是一款基于浏览器的可视化工具,无需其他客户端即可测试你的 MCP 工具。

npm run inspector

这将运行 npx @modelcontextprotocol/inspector node build/index.js 并在浏览器中打开 Inspector。在那里你可以:

  1. 点击 工具 标签页

  2. 选择工具 echo 或 call_my_api

  3. 填写参数,点击 运行工具,查看结果

5. 直接在 VS Code 中使用

已准备好 .vscode/mcp.json 文件。打开命令面板 → MCP: List Servers → 选择 my-mcp-server → Start Server。该服务器会自动通过 envFile 读取 .env。

项目结构

├── .env                  # rahasia kamu (tidak di-commit)
├── .env.example          # contoh isi .env
├── .vscode/mcp.json      # konfigurasi MCP untuk VS Code
├── src/
│   ├── config.ts         # loader environment variable
│   ├── apiClient.ts      # modul pemanggilan API eksternal (pakai API key & URL)
│   └── index.ts          # server MCP utama + daftar tool
├── package.json
└── tsconfig.json

添加新工具

在 src/index.ts 中添加新的 server.registerTool("nama_tool", { ... }, async (input) => { ... }) 代码块,然后重新运行 npm run build。

故障排查

  • 错误:“环境变量 ... 未填写” → 请确保 .env 文件已存在并已填写。

  • 工具 call_my_api 失败 → 检查 MY_API_BASE_URL 和 MY_API_KEY 是否正确,以及 API 端点是否支持 Authorization: Bearer <key> 请求头。

  • Inspector 无法打开 → 请确保已先运行 npm run build。

Related MCP Connectors

Related MCP Servers

  • F
    license
    Not graded
    quality
    D
    maintenance
    Enables users to expose arbitrary HTTP APIs as configurable MCP tools by defining tool specifications in a JSON configuration file.
    2,323 npm
    3
    -
  • F
    license
    Not graded
    quality
    D
    maintenance
    Enables AI clients to connect to example tools, resources, and prompts over MCP, demonstrating integration with IDEs, chatbots, and agent frameworks.
    -
  • A
    license
    Not graded
    quality
    C
    maintenance
    Enables interaction with Infor systems by exposing verified HTTP/SOAP requests as MCP tools, supporting multiple services, configurable authentication, and extensible request definitions.
    Apache 2.0