Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
39 changes: 39 additions & 0 deletions examples/typescript/agent/agent-claude.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
import { getTemperature, getTemperatureWorkflow } from './workflow';
import { query } from '@anthropic-ai/claude-agent-sdk';
import { createSdkMcpServer } from '@anthropic-ai/claude-agent-sdk';

async function main() {
// Generate a tool from a standalone task
// const temperatureTool = getTemperature.mcpTool('claude');

// Or from a workflow
const temperatureTool = getTemperatureWorkflow.mcpTool('claude');

// Wrap the tool in an in-process MCP server
const weatherServer = createSdkMcpServer({
name: 'weather',
version: '1.0.0',
tools: [temperatureTool],
});

for await (const message of query({
prompt: "What's the temperature in San Francisco?",
options: {
mcpServers: { weather: weatherServer },
allowedTools: [`mcp__${weatherServer.name}__${temperatureTool.name}`],
},
})) {
// "result" is the final message after all tool calls complete
if (message.type === 'result' && message.subtype === 'success') {
console.log(message.result);
}
}
}

if (require.main === module) {
main()
.catch(console.error)
.finally(() => {
process.exit(0);
});
}
30 changes: 30 additions & 0 deletions examples/typescript/agent/agent-openai.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
import { getTemperature, getTemperatureWorkflow } from './workflow';

async function main() {
// Generate a tool from a standalone task
// const temperatureTool = getTemperature.mcpTool('openai');

// Or from a workflow.
// mcpTool validates Zod v4 is installed before loading @openai/agents, so call it first
// to get a clear error message if the wrong Zod version is present.
const temperatureTool = getTemperatureWorkflow.mcpTool('openai');

// Dynamic import — @openai/agents crashes at load time with Zod v3, so we delay
// the import until after mcpTool has already verified Zod v4 is available.
const { Agent, run } = await import('@openai/agents');

const agent = new Agent({
name: 'Agent',
tools: [temperatureTool],
});
const result = await run(agent, 'What is the weather in San Francisco?');
console.log(result.finalOutput);
}

if (require.main === module) {
main()
.catch(console.error)
.finally(() => {
process.exit(0);
});
}
14 changes: 14 additions & 0 deletions examples/typescript/agent/worker.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
import { hatchet } from '../hatchet-client';
import { getTemperature, getTemperatureWorkflow } from './workflow';

async function main() {
const worker = await hatchet.worker('temperature-worker', {
workflows: [getTemperature, getTemperatureWorkflow],
});

await worker.start();
}

if (require.main === module) {
main();
}
47 changes: 47 additions & 0 deletions examples/typescript/agent/workflow.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
// > Declaring a Task
import { hatchet } from '../hatchet-client';
import { z } from 'zod/v4';

// Note that for agent tools, Zod must be used to create the input and output types for workflows/tasks
const TemperatureCoordinates = z.object({
latitude: z.number(),
longitude: z.number(),
});

const TemperatureInput = z.object({
locationName: z.string(),
coords: TemperatureCoordinates,
});

export type TemperatureInputWithZod = z.infer<typeof TemperatureInput>;

const temperatureRequest = async (input: TemperatureInputWithZod) => {
const response = await fetch(
`https://api.open-meteo.com/v1/forecast?latitude=${input.coords.latitude}&longitude=${input.coords.longitude}&current=temperature_2m&temperature_unit=fahrenheit`
);
const data: any = await response.json();

return {
text: `Temperature: ${data.current.temperature_2m}°F`,
};
};

export const getTemperature = hatchet.task({
name: 'getTemperature',
retries: 3,
fn: temperatureRequest,
inputValidator: TemperatureInput,
description: 'Get the current temperature at a location',
});

export const getTemperatureWorkflow = hatchet.workflow<TemperatureInputWithZod>({
name: 'getTemperatureWorkflow',
inputValidator: TemperatureInput,
description: 'Get the current temperature at a location',
});

getTemperatureWorkflow.task({
name: 'getTemperature',
fn: temperatureRequest,
});