AI2026년 3월 1일2분 읽기

MCP 서버 실전 — 데이터베이스, API, 파일시스템 연동

YS
YoungSam
조회 1488

DB 쿼리 MCP 서버

import { McpServer } from "@modelcontextprotocol/sdk/server/mcp.js";

const server = new McpServer({ name: "db-tools", version: "1.0.0" });

server.tool("query_db",
  { sql: z.string(), params: z.array(z.any()).optional() },
  async ({ sql, params }) => {
    // 읽기 전용 쿼리만 허용
    if (!sql.trim().toUpperCase().startsWith("SELECT")) {
      throw new Error("Only SELECT queries allowed");
    }
    const result = await db.query(sql, params);
    return { content: [{ type: "text", text: JSON.stringify(result.rows) }] };
  }
);

리소스 노출

MCP 리소스로 스키마 정보를 AI에 제공할 수 있습니다.

MCPServerIntegration

댓글 0

아직 댓글이 없습니다.