From e88c6f81968201416d817cc672338ec455cc7cb5 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=D0=94=D0=B0=D0=BD=D0=B8=D0=B8=D0=BB=20=D0=92=D0=B8=D0=BA?= =?UTF-8?q?=D1=82=D0=BE=D1=80=D0=BE=D0=B2?= Date: Tue, 30 Jun 2026 17:40:56 +0000 Subject: [PATCH] =?UTF-8?q?add:=20main.py=20=E2=80=94=20MCP-=D1=81=D0=B5?= =?UTF-8?q?=D1=80=D0=B2=D0=B5=D1=80=20=D0=B4=D0=BB=D1=8F=20=D1=83=D0=BF?= =?UTF-8?q?=D1=80=D0=B0=D0=B2=D0=BB=D0=B5=D0=BD=D0=B8=D1=8F=20=D0=BF=D0=B0?= =?UTF-8?q?=D0=BC=D1=8F=D1=82=D1=8C=D1=8E=20=D0=B0=D0=B3=D0=B5=D0=BD=D1=82?= =?UTF-8?q?=D0=B0?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- main.py | 74 +++++++++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 74 insertions(+) create mode 100644 main.py diff --git a/main.py b/main.py new file mode 100644 index 0000000..41bc3cd --- /dev/null +++ b/main.py @@ -0,0 +1,74 @@ +import os +import asyncio +from typing import Any, Dict + +from langchain_openai import ChatOpenAI +from langchain_core.messages import HumanMessage +from langchain.tools import tool +from deepagents import create_deep_agent +from deepagents.backends import CompositeBackend, LocalShellBackend, FilesystemBackend + +# LLM via OpenRouter +llm = ChatOpenAI( + model="openai/gpt-oss-20b:free", + base_url="https://openrouter.ai/api/v1", + api_key=os.getenv("OPENAI_API_KEY"), + temperature=0.0, +) + +backend = CompositeBackend( + [ + LocalShellBackend(workspace_dir="./workspace"), + FilesystemBackend(), + ] +) + +# Tool that forwards calls to the MCP memory server +@tool +def memory_action(action: str, params: Dict[str, Any]) -> str: + """ + Perform a memory operation via the MCP server. + + action: one of save_with_namespace, get_by_namespace, list_keys, delete, get, save + params: dictionary of parameters required by the chosen action. + Returns a JSON string with the server response. + """ + import json + import asyncio + from fastmcp import Client + + async def _call(): + client = Client("python memory_server.py") + await client.connect() + try: + result = await client.call_tool(action, params) + return json.dumps(result, ensure_ascii=False) + finally: + await client.close() + + loop = asyncio.get_event_loop() + return loop.run_until_complete(_call()) + + +agent = create_deep_agent( + model=llm, + tools=[memory_action], + backend=backend, + system_prompt="You are an assistant that can store and retrieve data using a remote memory server.", +) + +async def demo(): + # Example: store a name and then retrieve it + query = ( + "Save the user name 'Алексей' in the default namespace using the memory_action tool. " + "Then read back all entries from the default namespace and return them." + ) + result = await agent.ainvoke( + {"messages": [HumanMessage(content=query)]}, + {"configurable": {"thread_id": "demo-1"}}, + ) + print(result["messages"][-1].content) + + +if __name__ == "__main__": + asyncio.run(demo()) \ No newline at end of file