Add src/tools.py

This commit is contained in:
2026-06-04 23:24:07 +00:00
parent 34f4829f0e
commit 6f39128c4e
+36
View File
@@ -0,0 +1,36 @@
"""MCPstyle tool that fetches course metadata from a local JSON file.
The function `fetch_course_meta` performs a GET request to the mock MCP server
and returns the JSON content. It is wrapped with LangChain's `tool` decorator
so that the agent can call it.
"""
import json
from pathlib import Path
from typing import Dict
import httpx
from langchain.tools import tool
from .config import MCP_SERVER_URL
@tool("fetch_course_meta")
def fetch_course_meta(query: str) -> Dict:
"""Return course metadata that matches *query*.
The mock server simply returns the full JSON; the agent decides whether
the query is about metadata. The function is intentionally simple to
keep the focus on routing logic.
"""
try:
response = httpx.get(MCP_SERVER_URL, timeout=5.0)
response.raise_for_status()
data = response.json()
except Exception as exc: # pragma: no cover network errors
raise RuntimeError(f"Failed to fetch metadata: {exc}")
# Basic filtering return the whole object if the query contains a
# known keyword. In a real system this would be more sophisticated.
if any(k.lower() in query.lower() for k in data.keys()):
return data
# If nothing matches, return an empty dict
return {"message": "No metadata found for the query."}