Composio powers 1000+ toolkits, tool search, context management, authentication, and a sandboxed workbench to help you build AI agents that turn intent into action.

next
py 47 lines 1.63 KB
Raw
1 import asyncio
2 import os
3 import time
4
5 from langchain_mcp_adapters.client import MultiServerMCPClient
6 from langgraph.prebuilt import create_react_agent
7
8 from composio import Composio
9
10 composio = Composio()
11
12 # Slack auth config and user id (raise KeyError when unset)
13 slack_auth_config_id = os.environ["COMPOSIO_EXAMPLES_SLACK_AUTH_CONFIG_ID"]
14 user_id = os.environ["COMPOSIO_EXAMPLES_USER_ID"]
15
16 mcp_config = composio.mcp.create(
17 # Named `examples-<label>-<unix-seconds>` so the provisioning script's --gc
18 # can tell this throwaway config from one created by hand. The API caps
19 # this name at 30 characters, so the label stays short.
20 name=f"examples-lc-slack-{int(time.time())}",
21 toolkits=[{"toolkit": "slack", "auth_config_id": slack_auth_config_id}],
22 # Keep the exposed tool list small; LLM providers cap tools per request
23 allowed_tools=["SLACK_LIST_ALL_CHANNELS", "SLACK_SEARCH_MESSAGES"],
24 )
25
26 mcp_server = mcp_config.generate(user_id=user_id) # type: ignore[call-arg] # generate is typed as a bare Callable; runtime accepts the user_id keyword
27
28 client = MultiServerMCPClient(
29 {
30 "composio": {
31 "url": mcp_server["url"],
32 "transport": "streamable_http",
33 # The MCP endpoint authenticates with your Composio API key
34 "headers": {"x-api-key": os.environ["COMPOSIO_API_KEY"]},
35 }
36 }
37 )
38
39
40 async def langchain_mcp(message: str):
41 tools = await client.get_tools()
42 agent = create_react_agent("openai:gpt-4.1", tools)
43 response = await agent.ainvoke({"messages": message})
44 return response
45
46
47 mcp_response = asyncio.run(langchain_mcp("Show me 20 most used slack channels"))