"""공식 문서 검색 도구"""
from typing import Any, List
from mcp import types
from ..utils import search_naver, filter_official_docs
import json
class SearchDocsTool:
"""네이버 검색 + AI 필터링으로 공식 문서를 찾는 MCP 도구"""
@staticmethod
def get_tool_definition() -> types.Tool:
"""도구 정의 반환"""
return types.Tool(
name="search_official_docs",
description="네이버 검색 API를 사용하여 질문과 관련된 공식 문서를 찾아 URL을 반환합니다.",
inputSchema={
"type": "object",
"properties": {
"query": {
"type": "string",
"description": "검색할 질문 또는 키워드"
}
},
"required": ["query"]
}
)
@staticmethod
async def execute(arguments: dict[str, Any]) -> List[types.TextContent]:
"""도구 실행"""
query = arguments.get("query", "")
try:
# 네이버 검색 실행
search_results = search_naver(query)
# AI로 공식문서 필터링
result = filter_official_docs(query, search_results)
# JSON 형태로 데이터 구성
result_data = {
"title": result["title"],
"url": result["link"]
}
# JSON을 문자열로 변환하여 TextContent로 반환
return [types.TextContent(
type="text",
text=json.dumps(result_data, ensure_ascii=False, indent=2)
)]
except Exception as e:
return [types.TextContent(
type="text",
text=f"오류가 발생했습니다: {str(e)}"
)]