How To Build LangGraph Agents
A step-by-step guide to building stateful, autonomous SRE agents with MicroStax and LangGraph.
While the MicroStax MCP server is great for interactive, chat-based workflows, true automation requires stateful agents that can loop, retry, and manage complex operations without human intervention.
This guide shows you how to use our LangGraph integration to build an autonomous environment repair loop.
Prerequisites
- Node.js 18+
- An active MicroStax API token
- A Gemini API key (or another LLM provider compatible with LangChain)
1. Install Dependencies
Install the core LangChain/LangGraph libraries and the MicroStax tools package:
npm install @langchain/core @langchain/google-genai @langchain/langgraph @fabbrik/microstax-agents
2. Initialize the Tools
Create a new file agent.ts and initialize the MicroStax toolset:
import { getMicroStaxTools } from '@fabbrik/microstax-agents';
const microstaxTools = getMicroStaxTools({
apiKey: process.env.MICROSTAX_TOKEN,
baseUrl: 'https://api.microstax.ai'
});
// For demonstration, let's look at the available tools:
console.log(microstaxTools.map(t => t.name));
// Output: ['microstax_environment_list', 'microstax_diagnose', 'microstax_apply_remediation', ...]
3. Define the State
LangGraph agents are state machines. You define the "State" that is passed between nodes in the graph.
import { StateGraph, Annotation } from "@langchain/langgraph";
const AgentState = Annotation.Root({
failedEnvironmentId: Annotation<string>(),
diagnosis: Annotation<string>(),
remediationApplied: Annotation<boolean>(),
});
4. Build the Nodes
Create the individual functions (nodes) that will make up your workflow. Notice how these nodes will eventually invoke the MicroStax tools.
async function monitorNode(state: typeof AgentState.State) {
// Use microstax_environment_list to find failed environments
console.log("Monitoring for failures...");
return { failedEnvironmentId: "env-123" };
}
async function diagnoseNode(state: typeof AgentState.State) {
// Use microstax_diagnose
console.log(`Diagnosing ${state.failedEnvironmentId}...`);
return { diagnosis: "OOMKilled" };
}
async function repairNode(state: typeof AgentState.State) {
// Use microstax_apply_remediation
console.log(`Applying fix for ${state.diagnosis}...`);
return { remediationApplied: true };
}
5. Wire the Graph
Connect the nodes using LangGraph. This is where you define the autonomous loop.
const workflow = new StateGraph(AgentState)
.addNode("monitor", monitorNode)
.addNode("diagnose", diagnoseNode)
.addNode("repair", repairNode)
// Define the flow
.addEdge("__start__", "monitor")
.addEdge("monitor", "diagnose")
.addEdge("diagnose", "repair")
.addEdge("repair", "monitor"); // Loop back to monitor
const app = workflow.compile();
6. Run the Agent
Execute the graph and watch the agent perform the loop:
async function run() {
const result = await app.invoke({
failedEnvironmentId: "",
diagnosis: "",
remediationApplied: false
});
console.log("Agent finished run.");
}
run();
Why This Matters
By using LangGraph, you are moving away from fragile prompt-engineering and towards robust state machines. The agent remembers the environment ID, it remembers the diagnosis, and it knows exactly what step to take next. If the repair fails, the state machine can loop back to the diagnosis node with the new error context.