Skip to content

Commit 858eba2

Browse files
committed
added MCP example with strands agents
1 parent c0f13b1 commit 858eba2

21 files changed

+876
-0
lines changed

mcp_neo4j_client.py

Lines changed: 51 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,51 @@
1+
# mcp_client_example_mistral.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+
7+
# IMPORT MODELLO MISTRAL
8+
from strands.models.mistral import MistralModel
9+
import os
10+
MISTRAL_API_KEY = os.getenv("MISTRAL_API_KEY")
11+
12+
def create_streamable_http_transport():
13+
# L’URL deve essere quello del tuo server FastMCP
14+
return streamablehttp_client("http://localhost:8000/mcp/")
15+
16+
17+
def main():
18+
# 1) Crea client MCP
19+
streamable_http_mcp_client = MCPClient(create_streamable_http_transport)
20+
21+
with streamable_http_mcp_client:
22+
# 2) Lista tool MCP
23+
tools = streamable_http_mcp_client.list_tools_sync()
24+
print("TOOLS disponibili:", [getattr(t, "name", t.mcp_tool.name) for t in tools])
25+
26+
# 3) Crea l'agente con Mistral
27+
agent = Agent(
28+
tools=tools,
29+
model=MistralModel(
30+
api_key=MISTRAL_API_KEY,
31+
model_id="mistral-small-latest",
32+
temperature=0.7,
33+
max_tokens=200,
34+
),
35+
)
36+
37+
# 4) Esegui domanda tramite agente
38+
response = agent("What is 125 plus 375?")
39+
print("Risposta agente:", response)
40+
41+
# 5) Oppure invoca il tool direttamente
42+
result = streamable_http_mcp_client.call_tool_sync(
43+
tool_use_id="tool-1",
44+
name="neo4j_query",
45+
arguments={"x": 125, "y": 375},
46+
)
47+
print("Risultato calcolatore:", result["content"][0]["text"])
48+
49+
50+
if __name__ == "__main__":
51+
main()

mcp_neo4j_client2.py

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
from mcp.client.streamable_http import streamablehttp_client
2+
from mcp.client import MCPClient
3+
4+
def create_transport():
5+
return streamablehttp_client("http://localhost:8000/mcp")
6+
7+
if __name__ == "__main__":
8+
client = MCPClient(create_transport)
9+
with client:
10+
tools = client.list_tools_sync()
11+
print("TOOLS disponibili dal server MCP:", [t.name for t in tools])
12+
13+
# Esegui una query di test su Neo4j
14+
query = "MATCH (n) RETURN n LIMIT 5"
15+
result = client.call_sync("neo4j_query", query)
16+
print("RISULTATO QUERY:\n", result)

mcp_neo4j_client3.py

Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
# mcp_client_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+
7+
def create_streamable_http_transport():
8+
# Nota: l'URL deve corrispondere a quello usato dal tuo server FastMCP
9+
return streamablehttp_client("http://localhost:8000/mcp/")
10+
11+
def main():
12+
# Costruisci il client MCP di Strands
13+
streamable_http_mcp_client = MCPClient(create_streamable_http_transport)
14+
15+
# Usa il client in un context manager
16+
with streamable_http_mcp_client:
17+
tools = streamable_http_mcp_client.list_tools_sync()
18+
print("TOOLS disponibili:", [getattr(t, "name", t.mcp_tool.name) for t in tools])
19+
20+
# Crea l'agente Strands con quegli strumenti
21+
agent = Agent(tools=tools)
22+
23+
# Chiedi qualcosa all'agente che usa il tool "add"
24+
response = agent("What is 125 plus 375?")
25+
print("Risposta agente:", response)
26+
27+
# Oppure invoca direttamente il tool
28+
result = streamable_http_mcp_client.call_tool_sync(
29+
tool_use_id="tool-1",
30+
name="add",
31+
arguments={"x": 125, "y": 375}
32+
)
33+
print("Risultato calcolatore:", result["content"][0]["text"])
34+
35+
if __name__ == "__main__":
36+
main()

mcp_neo4j_mistral_example.py

Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
from strands import Agent
2+
from mcp.client.streamable_http import streamablehttp_client
3+
from strands.tools.mcp.mcp_client import MCPClient
4+
from strands.models.mistral import MistralModel
5+
6+
def create_streamable_http_transport():
7+
return streamablehttp_client("http://localhost:8000/mcp/")
8+
9+
def main():
10+
mcp_client = MCPClient(create_streamable_http_transport)
11+
12+
with mcp_client:
13+
tools = mcp_client.list_tools_sync()
14+
print("TOOLS disponibili:", [getattr(t, "name", t.mcp_tool.name) for t in tools])
15+
16+
# Agente Mistral con prompt system che forza l'uso del tool
17+
agent = Agent(
18+
tools=tools,
19+
model=MistralModel(
20+
api_key="aDHQXyGQcIP4uNSdI6vc24YA8vjkLr22",
21+
model_id="mistral-small-latest",
22+
max_tokens=200,
23+
temperature=0,
24+
stream=False
25+
),
26+
system_prompt=(
27+
"Se ricevi un'operazione da eseguire, non calcolarla da solo. "
28+
"Usa sempre il tool Neo4j `neo4j_query` per ottenere il risultato. "
29+
"Non scrivere numeri direttamente, sempre tramite il tool."
30+
)
31+
)
32+
33+
# Esempio: somma tramite query Neo4j
34+
query = "RETURN 125 + 375 AS result"
35+
response = agent(f"Esegui la query: {query}")
36+
print("Risposta agente:", response)
37+
38+
if __name__ == "__main__":
39+
main()

mcp_neo4j_mistral_example1.py

Lines changed: 52 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,52 @@
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+
def create_streamable_http_transport():
9+
# URL del tuo server FastMCP
10+
return streamablehttp_client("http://localhost:8000/mcp/")
11+
12+
def main():
13+
# 1️⃣ Costruisci il client MCP
14+
streamable_http_mcp_client = MCPClient(create_streamable_http_transport)
15+
16+
with streamable_http_mcp_client:
17+
# 2️⃣ Lista dei tool disponibili
18+
tools = streamable_http_mcp_client.list_tools_sync()
19+
print("TOOLS disponibili:", [getattr(t, "name", t.mcp_tool.name) for t in tools])
20+
21+
# 3️⃣ Crea l'agente con modello Mistral
22+
agent = Agent(
23+
tools=tools,
24+
model=MistralModel(
25+
api_key="aDHQXyGQcIP4uNSdI6vc24YA8vjkLr22", # <--- metti la tua chiave
26+
model_id="mistral-small-latest",
27+
max_tokens=200,
28+
temperature=0.7,
29+
)
30+
)
31+
32+
# 4️⃣ Esempio: Mistral usa il tool neo4j_query per calcolare 125+375
33+
# Nota: il prompt deve istruire l'agente a usare il tool
34+
prompt = """
35+
Compute 125 + 375 using the neo4j_query tool.
36+
Only use the tool, do not calculate in your head.
37+
"""
38+
39+
response = agent(prompt)
40+
print("Risposta agente:", response)
41+
42+
# 5️⃣ Alternativa: chiamata diretta al tool (manuale)
43+
cypher_query = "RETURN 125 + 375 AS result"
44+
result = streamable_http_mcp_client.call_tool_sync(
45+
tool_use_id="tool-1",
46+
name="neo4j_query",
47+
arguments={"query": cypher_query}
48+
)
49+
print("Risultato Neo4j:", result["content"][0]["text"])
50+
51+
if __name__ == "__main__":
52+
main()

mcp_neo4j_mistral_example2.py

Lines changed: 62 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,62 @@
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()

mcp_neo4j_mistral_example3.py

Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,47 @@
1+
from mcp.client.streamable_http import streamablehttp_client
2+
from strands import Agent
3+
from strands.tools.mcp.mcp_client import MCPClient
4+
from strands.models.mistral import MistralModel
5+
6+
def create_streamable_http_transport():
7+
return streamablehttp_client("http://localhost:8000/mcp/")
8+
9+
def main():
10+
# 1) Costruisci il client MCP
11+
streamable_http_mcp_client = MCPClient(create_streamable_http_transport)
12+
13+
with streamable_http_mcp_client:
14+
# 2) Lista tools disponibili
15+
tools = streamable_http_mcp_client.list_tools_sync()
16+
print("TOOLS disponibili:", [t.mcp_tool.name for t in tools])
17+
18+
# 3) Crea l'agente con Mistral
19+
agent = Agent(
20+
tools=tools,
21+
model=MistralModel(
22+
api_key="aDHQXyGQcIP4uNSdI6vc24YA8vjkLr22",
23+
model_id="mistral-small-latest",
24+
max_tokens=200,
25+
temperature=0.7
26+
)
27+
)
28+
29+
# 4) Prompt strutturato: forza l'uso del tool
30+
prompt = """
31+
You must always use the 'neo4j_query' tool to execute any query.
32+
Do not calculate arithmetic yourself.
33+
Query to execute: RETURN 125 + 375 AS result
34+
"""
35+
response = agent(prompt)
36+
print("Risposta agente:", response)
37+
38+
# 5) Chiamata diretta al tool (opzionale)
39+
result = streamable_http_mcp_client.call_tool_sync(
40+
tool_use_id="tool-1",
41+
name="neo4j_query",
42+
arguments={"query": "RETURN 125 + 375 AS result"}
43+
)
44+
print("Risultato Neo4j:", result)
45+
46+
if __name__ == "__main__":
47+
main()

mcp_neo4j_server-without-neo4j.py

Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,46 @@
1+
from strands import Agent
2+
from strands.models.mistral import MistralModel
3+
from strands.tools.mcp import MCPClient
4+
import os
5+
6+
# Import del client HTTP MCP (streamable)
7+
try:
8+
from mcp.client.streamable_http import streamablehttp_client
9+
except ImportError:
10+
# fallback se la versione mcp è differente
11+
from mcp.client.http import http_client as streamablehttp_client
12+
13+
MISTRAL_API_KEY = os.getenv("MISTRAL_API_KEY")
14+
15+
def create_transport():
16+
# Qui indichi l'endpoint HTTP del tuo server MCP
17+
# Assicurati che il percorso corrisponda a quello usato da FastMCP (es. /mcp)
18+
return streamablehttp_client("http://localhost:8000/mcp")
19+
20+
mcp_client = MCPClient(create_transport)
21+
22+
if __name__ == "__main__":
23+
with mcp_client:
24+
tools = mcp_client.list_tools_sync()
25+
print("TOOLS disponibili dal server MCP:", [t.name for t in tools])
26+
27+
# Crea l’agente Strands con Mistral
28+
model = MistralModel(
29+
api_key=MISTRAL_API_KEY, # metti la tua chiave Mistral
30+
model_id="mistral-small-latest",
31+
max_tokens=200,
32+
temperature=0.7,
33+
)
34+
35+
agent = Agent(model=model, tools=tools)
36+
37+
# Prompt: chiedi di eseguire una query tramite il tool Neo4j
38+
prompt = "Esegui questa query Neo4j e dimmi il risultato: `MATCH (n) RETURN n LIMIT 5`"
39+
result = agent(prompt)
40+
41+
# Stampa il risultato restituito dall'agente
42+
# a seconda della versione di Strands, può essere result.output o str(result)
43+
try:
44+
print("RISULTATO:", result.output)
45+
except AttributeError:
46+
print("RISULTATO:", result)

0 commit comments

Comments
 (0)