|
| 1 | +# mcp_neo4j_mistral_example.py |
| 2 | + |
| 3 | +from mcp.client.streamable_http import streamablehttp_client |
| 4 | +from strands import Agent |
| 5 | +from strands.tools.mcp.mcp_client import MCPClient |
| 6 | +from strands.models.mistral import MistralModel |
| 7 | + |
| 8 | + |
| 9 | +def create_transport(): |
| 10 | + # Transport per comunicare col server MCP locale |
| 11 | + return streamablehttp_client("http://localhost:8000/mcp/") |
| 12 | + |
| 13 | + |
| 14 | +def main(): |
| 15 | + # 1️⃣ Configura client MCP |
| 16 | + client = MCPClient(create_transport) |
| 17 | + |
| 18 | + with client: |
| 19 | + # Lista tool disponibili |
| 20 | + tools = client.list_tools_sync() |
| 21 | + print("TOOLS disponibili:", [t.mcp_tool.name for t in tools]) |
| 22 | + |
| 23 | + # 2️⃣ Crea l'agente Mistral |
| 24 | + # Nota: aggiungiamo al prompt istruzioni per usare solo i tool |
| 25 | + agent = Agent( |
| 26 | + tools=tools, |
| 27 | + model=MistralModel( |
| 28 | + api_key="aDHQXyGQcIP4uNSdI6vc24YA8vjkLr22", # metti la tua chiave |
| 29 | + model_id="mistral-small-latest", |
| 30 | + max_tokens=200, |
| 31 | + temperature=0.0, |
| 32 | + system_prompt=( |
| 33 | + "You are a Neo4j assistant. " |
| 34 | + "You MUST always use the 'neo4j_query' tool to execute queries. " |
| 35 | + "Never answer arithmetic yourself." |
| 36 | + ) |
| 37 | + ) |
| 38 | + ) |
| 39 | + |
| 40 | + # 3️⃣ Prompt all'agente |
| 41 | + prompt = """ |
| 42 | + You must always use the 'neo4j_query' tool to execute any query. |
| 43 | + Do not answer arithmetic yourself. |
| 44 | +
|
| 45 | + Execute this query in Neo4j: RETURN 125 + 375 AS result |
| 46 | + """ |
| 47 | + |
| 48 | + # Invocazione dell'agente |
| 49 | + response = agent(prompt) |
| 50 | + print("Risposta agente:", response) |
| 51 | + |
| 52 | + # 4️⃣ Chiamata diretta al tool Neo4j (solo per verifica) |
| 53 | + neo4j_result = client.call_tool_sync( |
| 54 | + tool_use_id="tool-1", |
| 55 | + name="neo4j_query", |
| 56 | + arguments={"query": "RETURN 125 + 375 AS result"} |
| 57 | + ) |
| 58 | + print("Risultato Neo4j:", neo4j_result) |
| 59 | + |
| 60 | + |
| 61 | +if __name__ == "__main__": |
| 62 | + main() |
0 commit comments