MCP-api-service
What is nstanw api service
MCP API Service
Máy chủ Model Context Protocol (MCP) để tương tác với các API hệ thống nội bộ
Kiến Trúc Hệ Thống
Tổng Quan
MCP API Service là một máy chủ trung gian hoạt động theo giao thức Model Context Protocol (MCP), giúp kết nối Claude AI với các API hệ thống nội bộ. Hệ thống này:
- Nhận lệnh từ Claude: Người dùng yêu cầu thực hiện một tác vụ thông qua Claude
- Xử lý và chuyển đổi: MCP chuyển đổi các yêu cầu từ người dùng sang định dạng API nội bộ
- Gọi API: Thực hiện cuộc gọi đến các API nội bộ
- Trả kết quả: Kết quả được định dạng và trả về Claude để hiển thị cho người dùng
Cơ Chế Hoạt Động
Máy chủ MCP hoạt động bằng cách giao tiếp qua stdio (standard input/output):
- Standard Input (stdin): Nhận yêu cầu từ Claude (ví dụ: lệnh tìm kiếm nhân viên)
- Standard Output (stdout): Trả kết quả về cho Claude (ví dụ: thông tin nhân viên tìm được)
- Standard Error (stderr): Ghi log lỗi (ví dụ: lỗi kết nối đến API)
Quy trình khi một yêu cầu được gửi:
- Claude gửi yêu cầu dạng JSON qua stdin
- MCP Server xử lý yêu cầu và gọi API thích hợp
- MCP Server trả kết quả về cho Claude qua stdout
- Claude hiển thị kết quả cho người dùng
Tính Năng (Kịch Bản)
Các kịch bản hiện có:
check_connection
- Kiểm tra kết nối tới API serversearch_employee
- Tìm kiếm nhân viên theo tên hoặc mãregister_breakfast
- Đăng ký ăn sáng cho nhân viênupdate_hoa_chat
- Cập nhật thông tin hóa chất theo cachuyen_nhan_vien_thi_cong
- Chuyển nhân viên thi công giao khoán
Thêm Kịch Bản Mới
1. Thêm Endpoint
Định nghĩa endpoint mới trong src/config.ts
:
export const CONFIG = {
// ...existing code...
TOOLS: {
// ...existing code...
TEN_NHOM_API: {
ACTION_API: '/api/services/app/TenService/TenAction'
}
}
}
2. Thêm Interface
Tạo interface trong src/types.ts
cho dữ liệu đầu vào/đầu ra:
export interface TenActionInput {
Param1: string;
Param2: number;
// Các tham số khác...
}
3. Tạo Service Mới hoặc Thêm Vào Service Hiện Có
Tạo file mới trong src/services/
hoặc thêm vào service hiện có:
// src/services/ten-service.service.ts
import { TenActionInput } from '../types.js';
import { ApiClient } from '../utils/api-client.js';
import { Logger } from '../utils/logger.js';
import { CONFIG } from '../config.js';
export class TenService {
private logger = new Logger();
async tenAction(input: TenActionInput) {
try {
this.logger.debug('Calling ten action API', {
url: CONFIG.TOOLS.TEN_NHOM_API.ACTION_API,
input
});
const response = await ApiClient.post(
CONFIG.TOOLS.TEN_NHOM_API.ACTION_API,
null,
{ params: input }
);
this.logger.info('Action completed successfully', { input });
return response.result;
} catch (error) {
this.logger.error('Error performing action', { error, input });
throw error;
}
}
}
4. Cập Nhật Index
Trong src/index.ts
:
- Thêm service mới (nếu có)
- Định nghĩa tool mới trong danh sách tools
- Thêm case xử lý trong hàm handleToolCall
Phát Triển
Cài đặt thư viện:
npm install
Build server:
npm run build
Chạy chế độ phát triển với tự động build lại:
npm run watch
Gỡ Lỗi
Thách Thức
Việc gỡ lỗi MCP gặp khó khăn vì:
- Không thể đặt breakpoint như ứng dụng thông thường
- Khó theo dõi luồng dữ liệu vào/ra
- Log lỗi có thể bị trộn lẫn với output
Giải Pháp
Sử dụng MCP Inspector để:
- Theo dõi yêu cầu gửi đến máy chủ
- Xem kết quả trả về của từng yêu cầu
- Kiểm tra log lỗi riêng biệt
- Giám sát hiệu năng
Khởi động Inspector:
npm run inspector
Inspector sẽ cung cấp URL truy cập giao diện web để theo dõi hoạt động của hệ thống.
Best Practices
-
Nhất Quán
- Tuân thủ cấu trúc đặt tên hiện có
- Sử dụng mẫu code đã được thiết lập
-
Xử Lý Lỗi
- Luôn implement try/catch
- Log đầy đủ thông tin lỗi
- Trả về thông báo lỗi rõ ràng
-
Logging
- Log đầy đủ các bước: debug trước khi gọi API, info khi thành công
- Bao gồm đầy đủ parameters trong log
-
Validation
- Kiểm tra tham số bắt buộc
- Validate kiểu dữ liệu của input
Xem thêm chi tiết trong docs/add-new-scenario.md
.
Leave a Comment
Frequently Asked Questions
What is MCP?
MCP (Model Context Protocol) is an open protocol that standardizes how applications provide context to LLMs. Think of MCP like a USB-C port for AI applications, providing a standardized way to connect AI models to different data sources and tools.
What are MCP Servers?
MCP Servers are lightweight programs that expose specific capabilities through the standardized Model Context Protocol. They act as bridges between LLMs like Claude and various data sources or services, allowing secure access to files, databases, APIs, and other resources.
How do MCP Servers work?
MCP Servers follow a client-server architecture where a host application (like Claude Desktop) connects to multiple servers. Each server provides specific functionality through standardized endpoints and protocols, enabling Claude to access data and perform actions through the standardized protocol.
Are MCP Servers secure?
Yes, MCP Servers are designed with security in mind. They run locally with explicit configuration and permissions, require user approval for actions, and include built-in security features to prevent unauthorized access and ensure data privacy.
Related MCP Servers
Brave Search MCP
Integrate Brave Search capabilities into Claude through MCP. Enables real-time web searches with privacy-focused results and comprehensive web coverage.
chrisdoc hevy mcp
sylphlab pdf reader mcp
An MCP server built with Node.js/TypeScript that allows AI agents to securely read PDF files (local or URL) and extract text, metadata, or page counts. Uses pdf-parse.
aashari mcp server atlassian bitbucket
Node.js/TypeScript MCP server for Atlassian Bitbucket. Enables AI systems (LLMs) to interact with workspaces, repositories, and pull requests via tools (list, get, comment, search). Connects AI directly to version control workflows through the standard MCP interface.
aashari mcp server atlassian confluence
Node.js/TypeScript MCP server for Atlassian Confluence. Provides tools enabling AI systems (LLMs) to list/get spaces & pages (content formatted as Markdown) and search via CQL. Connects AI seamlessly to Confluence knowledge bases using the standard MCP interface.
prisma prisma
Next-generation ORM for Node.js & TypeScript | PostgreSQL, MySQL, MariaDB, SQL Server, SQLite, MongoDB and CockroachDB
Zzzccs123 mcp sentry
mcp sentry for typescript sdk
zhuzhoulin dify mcp server
zhongmingyuan mcp my mac
zhixiaoqiang desktop image manager mcp
MCP 服务器,用于管理桌面图片、查看详情、压缩、移动等(完全让Trae实现)
Submit Your MCP Server
Share your MCP server with the community
Submit Now