Tools are functions that AI models can call to perform actions or retrieve dynamic data. They represent executable capabilities that extend what an AI can do.
@server.prompt( name="greeting", description="Generate a personalized greeting",)async def greeting_prompt( name: str, language: str = "en",) -> str: greetings = { "en": f"Hello {name}!", "es": f"Β‘Hola {name}!", "fr": f"Bonjour {name}!", } return greetings.get(language, greetings["en"])@server.prompt( name="code-review", description="Review code for issues",)async def code_review_prompt( code: str, language: str, focus: str = "all",) -> str: return f"""Please review this {language} code with a focus on {focus}:```{language}{code}
Provide:
Issues found
Suggestions for improvement
Best practices recommendations
"""
### When to Use PromptsPrompts are ideal for:- π **Standardizing** common instructions- π **Reusing** complex prompt templates- π― **Guiding** AI model behavior consistently- π¦ **Packaging** domain expertise as templates- π **Chaining** multiple AI interactions## Best Practices<AccordionGroup> <Accordion title="Tool Design"> β **Do:** - Keep tools focused on a single responsibility - Provide clear, descriptive names and descriptions - Use schema validation for all inputs - Return structured, predictable outputs - Include progress updates for long operations β **Don't:** - Create "god tools" that do too many things - Use vague or ambiguous names - Return inconsistent data structures - Expose dangerous operations without safeguards </Accordion> <Accordion title="Resource Design"> β **Do:** - Use clear, hierarchical URI patterns - Provide accurate MIME types - Cache expensive resource computations - Document URI template parameters - Support resource listing β **Don't:** - Use resources for actions with side effects - Return different data types for same URI - Expose sensitive data without authentication - Create deeply nested URI hierarchies </Accordion> <Accordion title="Prompt Design"> β **Do:** - Write clear, specific instructions - Include examples when helpful - Parameterize variable parts - Structure prompts for easy reading - Test prompts with actual AI models β **Don't:** - Make prompts too rigid or prescriptive - Include redundant or conflicting instructions - Assume specific model capabilities - Embed sensitive information in templates </Accordion> <Accordion title="Error Handling"> Always handle errors gracefully and return structured error information. Use try-catch blocks and return structured error objects with success flags, error messages, and error codes. This allows clients to properly handle and display errors to users. </Accordion></AccordionGroup>## Examples### Complete Server with All Three Primitives```typescriptimport { MCPServer, text, object, markdown } from "mcp-use/server";import { z } from "zod";const server = new MCPServer({ name: "complete-example", version: "1.0.0",});// TOOL: Perform actionsserver.tool({ name: "create_task", description: "Create a new task", schema: z.object({ title: z.string(), priority: z.enum(["low", "medium", "high"]), }),}, async ({ title, priority }) => { const task = await db.createTask({ title, priority }); return object(task);});// RESOURCE: Access dataserver.resource({ name: "task-list", uri: "app://tasks", title: "All Tasks",}, async () => { const tasks = await db.getAllTasks(); return object(tasks);});server.resource({ name: "task-detail", uri: "app://tasks/{taskId}", title: "Task Details",}, async ({ taskId }) => { const task = await db.getTask(taskId); return object(task);});// PROMPT: Guide AIserver.prompt({ name: "prioritize-tasks", description: "Help prioritize tasks", schema: z.object({ tasks: z.array(z.string()), }),}, async ({ tasks }) => { return text(`Please help prioritize these tasks based on urgency and importance:${tasks.map((t, i) => `${i + 1}. ${t}`).join("\n")}For each task, suggest:- Priority level (low/medium/high)- Reasoning- Estimated effort `);});await server.listen(3000);