Я пробовал создавать агентов, пробовал "llama-index-tools-mcp", который предположительно должен быть мостом от LLM к инструментам - безуспешно.
Мой main.py, вопрос, заданный пользователем:
Код: Выделить всё
@app.post("/query")
def query_ai(q: Query):
start = time.time() # How long the whole process takes
results = doc_search.query(q.question, k=5)
context = "\n\n".join([get_content(r) for r in results])
files = [get_filename(r) for r in results]
elapsed = round(time.time() - start, 2)
answer_text = ask_llama3(context, q.question, MCP_TOOLS)
return {"answer": answer_text.strip(), "files": files, "time in seconds": elapsed}
Код: Выделить всё
# Patch BasicMCPClient to ignore 'cursor' parameter to avoid an error
orig_list_tools = BasicMCPClient.list_tools
async def list_tools_no_cursor(self, *args, **kwargs):
kwargs.pop("cursor", None)
return await orig_list_tools(self, *args, **kwargs)
BasicMCPClient.list_tools = list_tools_no_cursor
# Define MCP server endpoint
MCP_ENDPOINT = "http://127.0.0.1:8001/mcp"
# Asynchronously load MCP tools from the MCP server
async def load_tools_async():
print("[INFO] Loading MCP tools from server (HTTP)...")
client = BasicMCPClient(MCP_ENDPOINT) # Create an HTTP client
tools = await load_mcp_tools(client) # Load all available MCP tools from the server
for tool in tools:
print(f"[INFO] Loaded MCP tool: {tool.name}")
return tools
# Helper to load tools synchronously (for non-async contexts)
def load_tools() -> List:
return asyncio.run(load_tools_async())
Код: Выделить всё
mcp = FastMCP("My Local File AI MCP Server")
# test tool
@mcp.tool(name="test_server1", description="Just a simple test tool that does nothing")
def test_server1():
print("[DEBUG] this method does nothing...")
return {"message": "This is a MCP tool response."}
Код: Выделить всё
def ask_llama3(context, question, MCP_TOOLS):
prompt = (
f"Use the following context from local documents to answer:\n\n"
f"{context}\n\n"
f"Question: {question}\n"
f"You also have the following tools available: {MCP_TOOLS}\n"
Подробнее здесь: https://stackoverflow.com/questions/798 ... -mcp-tools
Мобильная версия