
Claude Code SDK là bộ công cụ chính thức từ Anthropic, cho phép lập trình viên tích hợp Claude trực tiếp vào quy trình coding — từ IDE, CLI, đến CI/CD. Không chỉ là chatbot, đây là AI coding assistant có khả năng đọc codebase, chạy shell command, và tự động refactor.
Claude Code SDK là gì?
Claude Code SDK cung cấp hai interface chính:
- CLI tool –
claudecommand chạy trong terminal, tự động scan context cwd, git diff, và environment variables. - API SDK –
@anthropic-ai/claude-codepackage, dùng trong Node.js scripts để programmatically invoke Claude với tools.
Cài đặt và cấu hình nhanh
1. Install CLI
npm install -g @anthropic-ai/claude-code
# hoặc với pnpm
pnpm add -g @anthropic-ai/claude-code
# Authenticate với Anthropic API key
claude auth login

2. Cấu hình MCP servers
Claude Code hỗ trợ MCP (Model Context Protocol) để kết nối external tools:
{
"mcpServers": {
"filesystem": {
"command": "npx",
"args": ["-y", "@modelcontextprotocol/server-filesystem", "/path/to/project"]
},
"github": {
"command": "npx",
"args": ["-y", "@modelcontextprotocol/server-github"],
"env": {
"GITHUB_TOKEN": "ghp_xxx"
}
},
"postgres": {
"command": "npx",
"args": ["-y", "@modelcontextprotocol/server-postgres"],
"env": {
"DATABASE_URL": "postgres://user:***@localhost/db"
}
}
}
}
MCP servers giúp Claude access file system, query database, hoặc tạo GitHub PR — tất cả trong cùng một session.
Sử dụng CLI trong thực tế
Interactive mode
cd /path/to/your/project
claude
Claude tự động:
- Đọc
README.md,package.json, file structure - Hiểu git status + diff
- Suggest code changes theo best practices
- Chạy
npm testhoặcpytestđể verify
One-shot command
claude "Refactor auth middleware to use JWT with refresh tokens"
claude --model claude-opus-4-5 "Write unit tests for utils/validation.ts"
Claude sẽ đọc file, hiểu logic hiện tại, rồi suggest patch hoặc write new code.

API SDK: Tích hợp vào CI/CD
import { ClaudeCode } from '@anthropic-ai/claude-code';
const claude = new ClaudeCode({
apiKey: process.env.ANTHROPIC_API_KEY,
model: 'claude-opus-4-5'
});
const result = await claude.run({
prompt: 'Review this PR for security vulnerabilities',
context: {
files: ['src/auth.ts', 'src/middleware.ts'],
diff: gitDiff
},
tools: ['read_file', 'run_command']
});
console.log(result.output);
SDK này dùng để:
- Automated code review – Chạy trên mỗi PR, gửi feedback comment.
- Auto-fix lint errors – Đọc ESLint output, generate fix PR.
- Generate changelog – Phân tích git log, viết markdown changelog.
So sánh Claude Code vs Cursor vs Copilot
| Tính năng | Claude Code | Cursor | GitHub Copilot |
|---|---|---|---|
| Context window | 200k tokens | 128k tokens | 8k tokens |
| Read entire codebase | Yes | Yes (pro) | No |
| Run shell commands | Yes | Limited | No |
| MCP support | Yes | No | No |
| Git-aware | Yes | Yes | Yes |
| CLI-first | Yes | No | No |
Claude Code mạnh nhất cho workflow automation và multi-step reasoning. Cursor tốt hơn cho real-time autocomplete. Copilot lightweight nhưng thiếu context dài.
Best practices
- Luôn cung cấp
README.mdvàCONTRIBUTING.md– Claude dùng làm context chính. - Dùng MCP servers thay vì hardcode API credentials.
- Chạy
claude --dry-runtrước khi apply changes lớn. - Pin model version trong CI:
--model claude-opus-4-5-20250514.
