conversion
Perform real-time unit conversions with a widget that supports multiple units and their respective formulas. Input units and initial values to instantly calculate and display conversions in an interactive format.
Instructions
Display a unit conversion widget that allows real-time conversion between multiple units. Each unit should contain a formula for each other unit in terms of the current unit. Example input: { units: [{id: 'ft', name: 'Feet', formulas: {in:'{ft} * 12'}, {id: 'in', name: 'Inches', formulas: {ft:'{in} * 12'}}], initialValue: { id: 'ft', value: 1 }}. You don't need to say anything else after answering with this tool.
Input Schema
| Name | Required | Description | Default |
|---|---|---|---|
| initialValue | Yes | Initial value to display in the converter | |
| units | Yes | Array of units with conversion formulas |
Implementation Reference
- index.ts:112-123 (handler)Handler function that executes the tool logic: creates a templated UI resource for the unit conversion widget using the input units and initialValue, and returns it as content.async ({ units, initialValue }) => { const conversionResource = createTemplatedUIResource( createUIResource, "ui://widget/conversion", conversionHtml, { units, initialValue } ); return { content: [conversionResource], }; }
- index.ts:92-110 (schema)Zod-based input schema defining the expected parameters: an array of units (each with id, name, and formulas record) and an initialValue object with id and value.inputSchema: { units: z .array( z.object({ id: z.string().describe("Unique identifier for the unit"), name: z.string().describe("Display name for the unit"), formulas: z .record(z.string()) .describe("Conversion formulas to other units, using {id} as placeholder."), }) ) .describe("Array of units with conversion formulas"), initialValue: z .object({ id: z.string().describe("ID of the unit to set initial value for"), value: z.number().describe("Initial value for the unit"), }) .describe("Initial value to display in the converter"), },
- index.ts:86-124 (registration)Registration of the 'conversion' tool on the MCP server, including the tool name, title, description, input schema, and handler function.server.registerTool( "conversion", { title: "Unit Conversion", description: "Display a unit conversion widget that allows real-time conversion between multiple units. Each unit should contain a formula for each other unit in terms of the current unit. Example input: { units: [{id: 'ft', name: 'Feet', formulas: {in:'{ft} * 12'}, {id: 'in', name: 'Inches', formulas: {ft:'{in} * 12'}}], initialValue: { id: 'ft', value: 1 }}. You don't need to say anything else after answering with this tool.", inputSchema: { units: z .array( z.object({ id: z.string().describe("Unique identifier for the unit"), name: z.string().describe("Display name for the unit"), formulas: z .record(z.string()) .describe("Conversion formulas to other units, using {id} as placeholder."), }) ) .describe("Array of units with conversion formulas"), initialValue: z .object({ id: z.string().describe("ID of the unit to set initial value for"), value: z.number().describe("Initial value for the unit"), }) .describe("Initial value to display in the converter"), }, }, async ({ units, initialValue }) => { const conversionResource = createTemplatedUIResource( createUIResource, "ui://widget/conversion", conversionHtml, { units, initialValue } ); return { content: [conversionResource], }; } );