Overview
In this tutorial, you can learn how to migrate an existing LangGraph agent to the MongoDB Atlas Agent Engine. The tutorial shows how to add the MongoDB Atlas Agent Engine SDK to your project, wrap your agent code with the App class, create the required configuration files, and register your project on the Atlas Agent Engine.
Prerequisites
Before you begin, perform the following prerequisite tasks:
Install the
agentengineCLI and authenticate your account. To learn more, see Install and Authenticate.Open the
pyproject.tomlfile within your existing LangGraph agent project.Confirm that your
pyproject.tomlfile has a[build-system]block that sets therequiresandbuild-backendkeys. Theagentengine initcommand requires this block. You can runuv init --packagein your project root to generate a file that meets this requirement.Install uv.
Run Docker Desktop or Docker Engine on your machine.
Tutorial
Complete the following steps to add the platform SDK, update your agent code, and register your project on the Atlas Agent Engine.
Wrap your agent with the App class.
Update your agent's entry module to use the platform SDK by making the following changes:
Import
Appfromagent_engine_sdk_langgraphand create anAppinstance at the module level.Register each tool by using the
@app.tool()decorator.Add the
@app.entrypointdecorator to your agent build function.Wrap your LLM with the
app.llm()method to route LLM calls through the platform's audited execution path.Replace your checkpointer with
app.checkpointer()to use the platform's MongoDB-backed checkpointer.Call the
app.run()method at the bottom of the module.
The following example shows a complete migrated agent module:
from agent_engine_sdk_langgraph import App from langgraph.graph import StateGraph, MessagesState from langgraph.prebuilt import ToolNode from langchain_openai import ChatOpenAI app = App(app_name="my-agent") def lookup(query: str) -> str: """Search the knowledge base.""" return f"result for {query}" def build_agent(): llm = app.llm(ChatOpenAI(model="gpt-4o-mini")) tools = app.get_tools() def call_model(state: MessagesState): return {"messages": [llm.invoke(state["messages"])]} graph = StateGraph(MessagesState) graph.add_node("agent", call_model) graph.add_node("tools", ToolNode(tools)) graph.set_entry_point("agent") graph.add_edge("tools", "agent") return graph.compile(checkpointer=app.checkpointer()) app.run()
Create an agent.yaml file.
In your project root, create an agent.yaml file that contains the entrypoint field, as shown in the following code:
entrypoint: my_agent.main:app name: my-agent framework: langgraph
The entrypoint field must point to the App instance in module.path:attribute format.
Tip
To view all available agent.yaml fields, see Agent Contract Reference.
Create a .env file.
In your project root, create a .env file that specifies your MongoDB connection string and LLM provider API key, as shown in the following code:
MONGODB_URI=<your-mongodb-connection-string> <PROVIDER>_API_KEY=<your-api-key>
Important
The container reads secrets only from the .env file at runtime. Do not commit your secrets to your version control system.
Run the agent locally.
To confirm that your migrated agent runs correctly, start the local development environment by running the following command:
agentengine dev up
When the stack is ready, open http://localhost:3000 in your browser to interact with your agent.
Note
The UI might run on a different port than the default port 3000. To view your URL, check the ui field in the agentengine dev up command output.
Next Steps
After confirming that your migrated agent runs locally, you can perform the following tasks:
To provision secrets and build the agent image, see Provision Cloud Secrets.
To add platform features such as memory and guardrails, see Add Memory to Your Agent.