LangChain
Build a LangChain agent with Scalekit-authenticated Gmail tools. Scalekit returns native LangChain tool objects; no schema reshaping needed.
Build a LangChain agent that reads a user’s Gmail inbox. Scalekit handles OAuth, token storage, and returns tools in native LangChain format. Your agent code needs no Scalekit-specific logic beyond initialization.
Full code on GitHubInstall
Section titled “Install”pip install scalekit-sdk-python langchain-openaiInitialize
Section titled “Initialize”import osimport scalekit.client
scalekit_client = scalekit.client.ScalekitClient( client_id=os.getenv("SCALEKIT_CLIENT_ID"), client_secret=os.getenv("SCALEKIT_CLIENT_SECRET"), env_url=os.getenv("SCALEKIT_ENV_URL"),)actions = scalekit_client.actionsConnect the user to Gmail
Section titled “Connect the user to Gmail”response = actions.get_or_create_connected_account( connection_name="gmail", identifier="user_123",)if response.connected_account.status != "ACTIVE": link = actions.get_authorization_link(connection_name="gmail", identifier="user_123") print("Authorize Gmail:", link.link) input("Press Enter after authorizing...")See Authorize a user for production auth handling.
Build and run the agent
Section titled “Build and run the agent”actions.langchain.get_tools() returns native StructuredTool objects. Bind them to your LLM and run the tool-calling loop:
from langchain_openai import ChatOpenAIfrom langchain_core.messages import HumanMessage, ToolMessage
tools = actions.langchain.get_tools( identifier="user_123", connection_names=["gmail"],)tool_map = {t.name: t for t in tools}
llm = ChatOpenAI(model="gpt-4o").bind_tools(tools)messages = [HumanMessage("Fetch my last 5 unread emails and summarize them")]
while True: response = llm.invoke(messages) messages.append(response) if not response.tool_calls: print(response.content) break for tc in response.tool_calls: result = tool_map[tc["name"]].invoke(tc["args"]) messages.append(ToolMessage(content=str(result), tool_call_id=tc["id"]))Use MCP instead
Section titled “Use MCP instead”LangChain supports MCP via langchain-mcp-adapters. Install it, then connect to a Scalekit-generated MCP URL:
pip install langchain-mcp-adaptersimport asynciofrom langchain_mcp_adapters.client import MultiServerMCPClientfrom langchain_openai import ChatOpenAIfrom langchain_core.messages import HumanMessage, ToolMessage
async def run(mcp_url: str): async with MultiServerMCPClient( {"scalekit": {"transport": "streamable_http", "url": mcp_url}} ) as client: tools = client.get_tools() tool_map = {t.name: t for t in tools} llm = ChatOpenAI(model="gpt-4o").bind_tools(tools) messages = [HumanMessage("Fetch my last 5 unread emails and summarize them")]
while True: response = await llm.ainvoke(messages) messages.append(response) if not response.tool_calls: print(response.content) break for tc in response.tool_calls: result = await tool_map[tc["name"]].ainvoke(tc["args"]) messages.append(ToolMessage(content=str(result), tool_call_id=tc["id"]))
asyncio.run(run(mcp_url))See Generate user MCP URLs to get mcp_url.