Skip to content

Instantly share code, notes, and snippets.

@raine
Last active April 30, 2026 20:27
Show Gist options
  • Select an option

  • Save raine/67dfb38f5fbb3d2f971461a7f8b39caa to your computer and use it in GitHub Desktop.

Select an option

Save raine/67dfb38f5fbb3d2f971461a7f8b39caa to your computer and use it in GitHub Desktop.
import {
query,
tool,
createSdkMcpServer,
} from '@anthropic-ai/claude-agent-sdk';
import { z } from 'zod';
const rollDice = tool(
'roll_dice',
'Roll an N-sided die and return the result as a number',
{ sides: z.number().int().min(2).max(1000) },
async ({ sides }) => ({
content: [
{ type: 'text', text: String(1 + Math.floor(Math.random() * sides)) },
],
}),
{ annotations: { readOnlyHint: true } },
);
const toyServer = createSdkMcpServer({
name: 'toy',
version: '0.0.1',
tools: [rollDice],
});
const result = query({
prompt:
'Roll a 20-sided die using the roll_dice tool, then read package.json with the Read tool and tell me the package name. ' +
'Reply with exactly one line in the format: `<name> rolled <n>`.',
options: {
mcpServers: {
toy: toyServer,
},
allowedTools: ['Read', 'mcp__toy__roll_dice'],
permissionMode: 'dontAsk',
},
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment