Composio powers 1000+ toolkits, tool search, context management, authentication, and a sandboxed workbench to help you build AI agents that turn intent into action.
Mirrored from https://github.com/Camo67/composio.git
Synced Nov 21, 03:37 UTC
| 1 | # /// script |
| 2 | # requires-python = ">=3.12" |
| 3 | # dependencies = [ |
| 4 | # "composio>=0.17.1", |
| 5 | # "langchain[mcp]>=1.4,<2", |
| 6 | # "langchain-openai", |
| 7 | # ] |
| 8 | # /// |
| 9 | """Read a public Hacker News profile through a Composio MCP session. |
| 10 | |
| 11 | Set COMPOSIO_API_KEY, OPENAI_API_KEY, and COMPOSIO_EXAMPLES_USER_ID, then run |
| 12 | from the repository root: |
| 13 | uv run --script python/examples/tool_router/langchain_agent.py |
| 14 | |
| 15 | The script declares its dependencies so it runs independently of the SDK's |
| 16 | development environment. LangChain's MCPAdapter API is currently in beta. |
| 17 | """ |
| 18 | |
| 19 | import asyncio |
| 20 | import os |
| 21 | |
| 22 | from composio import Composio, SESSION_PRESET_DIRECT_TOOLS |
| 23 | from fastmcp.client.transports import StreamableHttpTransport |
| 24 | from langchain.agents import create_agent |
| 25 | from langchain.mcp import MCPAdapter |
| 26 | from langchain_openai import ChatOpenAI |
| 27 | |
| 28 | |
| 29 | async def main(): |
| 30 | composio = Composio(api_key=os.environ["COMPOSIO_API_KEY"]) |
| 31 | session = composio.create( |
| 32 | user_id=os.environ["COMPOSIO_EXAMPLES_USER_ID"], |
| 33 | toolkits=["hackernews"], |
| 34 | tools={"hackernews": {"enable": ["HACKERNEWS_GET_USER"]}}, |
| 35 | session_preset=SESSION_PRESET_DIRECT_TOOLS, |
| 36 | mcp=True, |
| 37 | ) |
| 38 | try: |
| 39 | transport = StreamableHttpTransport( |
| 40 | url=session.mcp.url, |
| 41 | headers=session.mcp.headers, |
| 42 | ) |
| 43 | async with MCPAdapter(transport) as adapter: |
| 44 | tools = await adapter.list_tools() |
| 45 | agent = create_agent( |
| 46 | model=ChatOpenAI(model="gpt-5.2"), |
| 47 | tools=tools, |
| 48 | ) |
| 49 | result = await agent.ainvoke( |
| 50 | { |
| 51 | "messages": [ |
| 52 | { |
| 53 | "role": "user", |
| 54 | "content": ( |
| 55 | "Look up the Hacker News user pg with the available tool. " |
| 56 | "Report their current karma and summarize their bio if present. " |
| 57 | "Include https://news.ycombinator.com/user?id=pg as the source. " |
| 58 | "If the lookup fails, report the error instead of guessing." |
| 59 | ), |
| 60 | } |
| 61 | ] |
| 62 | } |
| 63 | ) |
| 64 | print(result["messages"][-1].content) |
| 65 | finally: |
| 66 | session.delete() |
| 67 | |
| 68 | |
| 69 | if __name__ == "__main__": |
| 70 | asyncio.run(main()) |