diff --git a/client_mcp.py b/client_mcp.py new file mode 100644 index 0000000..1913242 --- /dev/null +++ b/client_mcp.py @@ -0,0 +1,47 @@ +import asyncio +import os +from dotenv import load_dotenv +from langchain_google_genai import ChatGoogleGenerativeAI + +# Imports spécifiques pour la connexion réseau (SSE) +from mcp.client.sse import sse_client +from mcp.client.session import ClientSession +from langchain_mcp_adapters.tools import load_mcp_tools + +# Chargement de la clé API +load_dotenv() + +async def lancer_client_reseau(): + print("🤖 Initialisation de Gemini...") + llm = ChatGoogleGenerativeAI(model="gemini-2.5-flash") + + # L'adresse de votre serveur + url = "http://127.0.0.1:9000/sse" + print(f"🔌 Connexion au serveur distant sur {url}...") + + # 1. On ouvre le canal de communication réseau + async with sse_client(url) as (read_stream, write_stream): + # 2. On démarre la session MCP sur ce canal + async with ClientSession(read_stream, write_stream) as session: + await session.initialize() + + # 3. Récupération et conversion des outils au format LangChain + outils = await load_mcp_tools(session) + print(f"✅ Outils détectés : {[outil.name for outil in outils]}") + + # 4. On équipe l'IA avec ces outils + llm_avec_outils = llm.bind_tools(outils) + + # --- LE TEST --- + question = "Quelles sont les tâches sur ma liste ? Ajoute ensuite la tâche 'Finir le projet réseau'." + print(f"\n🗣️ Question posée : {question}") + + reponse = await llm_avec_outils.ainvoke(question) + + if reponse.tool_calls: + print("\n⚙️ DÉCISION DE L'IA :") + for outil in reponse.tool_calls: + print(f"- Outil demandé : {outil['name']} | Paramètres : {outil['args']}") + +if __name__ == "__main__": + asyncio.run(lancer_client_reseau()) \ No newline at end of file diff --git a/requirements b/requirements index 6925f61..fc9542f 100644 --- a/requirements +++ b/requirements @@ -9,4 +9,6 @@ dotenv qdrant-client langchain-qdrant langchain-chain -docx2txt \ No newline at end of file +langchain_mcp_adapters +docx2txt +mcp \ No newline at end of file