Model Context Protocol 서버를 사용하여 사용자 정의 도구로 Claude Code를 확장하세요
.mcp.json
{ "mcpServers": { "filesystem": { "command": "npx", "args": ["@modelcontextprotocol/server-filesystem"], "env": { "ALLOWED_PATHS": "/Users/me/projects" } } } }
import { query } from "@anthropic-ai/claude-code"; for await (const message of query({ prompt: "내 프로젝트의 파일들을 나열해줘", options: { mcpConfig: ".mcp.json", allowedTools: ["mcp__filesystem__list_files"] } })) { if (message.type === "result" && message.subtype === "success") { console.log(message.result); } }
// .mcp.json 구성 { "mcpServers": { "my-tool": { "command": "node", "args": ["./my-mcp-server.js"], "env": { "DEBUG": "${DEBUG:-false}" } } } }
// SSE 서버 구성 { "mcpServers": { "remote-api": { "type": "sse", "url": "https://api.example.com/mcp/sse", "headers": { "Authorization": "Bearer ${API_TOKEN}" } } } } // HTTP 서버 구성 { "mcpServers": { "http-service": { "type": "http", "url": "https://api.example.com/mcp", "headers": { "X-API-Key": "${API_KEY}" } } } }
import { query } from "@anthropic-ai/claude-code"; // 사용 가능한 리소스 나열 for await (const message of query({ prompt: "데이터베이스 서버에서 사용 가능한 리소스는 무엇인가요?", options: { mcpConfig: ".mcp.json", allowedTools: ["mcp__list_resources", "mcp__read_resource"] } })) { if (message.type === "result") console.log(message.result); }
// 환경 변수가 포함된 .mcp.json { "mcpServers": { "secure-api": { "type": "sse", "url": "https://api.example.com/mcp", "headers": { "Authorization": "Bearer ${API_TOKEN}", "X-API-Key": "${API_KEY:-default-key}" } } } } // 환경 변수 설정 process.env.API_TOKEN = "your-token"; process.env.API_KEY = "your-key";
import { query } from "@anthropic-ai/claude-code"; for await (const message of query({ prompt: "데이터 처리", options: { mcpServers: { "data-processor": dataServer } } })) { if (message.type === "system" && message.subtype === "init") { // MCP 서버 상태 확인 const failedServers = message.mcp_servers.filter( s => s.status !== "connected" ); if (failedServers.length > 0) { console.warn("연결 실패:", failedServers); } } if (message.type === "result" && message.subtype === "error_during_execution") { console.error("실행 실패"); } }