LangChain Integration
Integrate MachineMarket as a LangChain JS tool so your agent can provision VPS instances on demand.
Prerequisites
- LangChain JS (
langchainand@langchain/core) - An LLM provider (OpenAI, Anthropic, etc.)
- The MachineMarket SDK
Create the tool
Subclass StructuredTool to define the input schema and execution logic:
tools/spawn-server.ts
import { StructuredTool } from "@langchain/core/tools";
import { z } from "zod";
import { MachineMarket } from "@machinemarket/sdk";
const mm = new MachineMarket({
baseUrl: "https://api.machinemarket.ai",
});
export class SpawnServerTool extends StructuredTool {
name = "spawn_server";
description = "Provision a VPS server with MachineMarket. " +
"Requires a USDC payment tx_hash on Base. " +
"Returns instance ID, IP address, and SSH credentials.";
schema = z.object({
tier: z.enum(["Nano", "Small", "Medium", "Large"])
.describe("Server tier"),
template: z.enum(["base", "node", "python", "agent"])
.describe("Software template"),
duration: z.enum(["1h", "24h", "7d", "30d"])
.describe("How long the server should run"),
tx_hash: z.string()
.describe("USDC payment transaction hash on Base"),
wallet_address: z.string()
.describe("Sender wallet address"),
region: z.string().optional()
.describe("Deployment region (default: fsn1)"),
});
async _call(input: z.infer<typeof this.schema>): Promise<string> {
try {
const result = await mm.spawn(input);
return JSON.stringify({
instance_id: result.instance.id,
status: result.instance.status,
ip_address: result.credentials.ssh_host,
ssh_user: result.credentials.ssh_user,
expires_at: result.instance.expires_at,
});
} catch (err) {
return JSON.stringify({ error: String(err) });
}
}
}Wire into an agent
agent.ts
import { ChatOpenAI } from "@langchain/openai";
import { createReactAgent } from "@langchain/langgraph/prebuilt";
import { SpawnServerTool } from "./tools/spawn-server";
const llm = new ChatOpenAI({ model: "gpt-4o" });
const tools = [new SpawnServerTool()];
const agent = createReactAgent({ llm, tools });
const result = await agent.invoke({
messages: [
{
role: "user",
content: "Spin up a Small Node.js server for 24 hours. " +
"I already paid with tx 0xabc...def from wallet 0x123...456.",
},
],
});
console.log(result.messages.at(-1)?.content);~Tip
You can add more tools for
getInstance, destroyInstance, etc. following the same pattern. Each tool wraps one SDK method.Full working example
Complete example
import { StructuredTool } from "@langchain/core/tools";
import { ChatOpenAI } from "@langchain/openai";
import { createReactAgent } from "@langchain/langgraph/prebuilt";
import { z } from "zod";
import { MachineMarket } from "@machinemarket/sdk";
const mm = new MachineMarket({
baseUrl: "https://api.machinemarket.ai",
});
class GetPricingTool extends StructuredTool {
name = "get_pricing";
description = "Get MachineMarket VPS pricing tiers and durations.";
schema = z.object({});
async _call(): Promise<string> {
const pricing = await mm.getPricing();
return JSON.stringify(pricing);
}
}
class SpawnServerTool extends StructuredTool {
name = "spawn_server";
description = "Provision a VPS. Requires USDC payment tx_hash.";
schema = z.object({
tier: z.enum(["Nano", "Small", "Medium", "Large"]),
template: z.enum(["base", "node", "python", "agent"]),
duration: z.enum(["1h", "24h", "7d", "30d"]),
tx_hash: z.string(),
wallet_address: z.string(),
});
async _call(input: z.infer<typeof this.schema>): Promise<string> {
const result = await mm.spawn(input);
return JSON.stringify(result);
}
}
class DestroyServerTool extends StructuredTool {
name = "destroy_server";
description = "Destroy a MachineMarket VPS instance.";
schema = z.object({
instance_id: z.string().describe("The instance UUID to destroy"),
});
async _call(input: z.infer<typeof this.schema>): Promise<string> {
const result = await mm.destroyInstance(input.instance_id);
return JSON.stringify(result);
}
}
const agent = createReactAgent({
llm: new ChatOpenAI({ model: "gpt-4o" }),
tools: [
new GetPricingTool(),
new SpawnServerTool(),
new DestroyServerTool(),
],
});Next steps
- SDK Reference — All available methods
- API Reference — Endpoint details
- CrewAI Guide — Alternative framework