|
| 1 | +--- |
| 2 | +title: "Annotations" |
| 3 | +description: "Metadata for enhancing client UX without affecting model context" |
| 4 | +--- |
| 5 | + |
| 6 | +# Annotations |
| 7 | + |
| 8 | +Annotations are a powerful feature in the Model Context Protocol (MCP) that allow servers to provide additional metadata about content without affecting what the model sees. They provide a way to separate UX concerns from the actual content presented to the model. |
| 9 | + |
| 10 | +## Overview |
| 11 | + |
| 12 | +Annotations serve as a bridge between what's useful for the user interface and what should be included in model context. This separation is important because: |
| 13 | + |
| 14 | +1. Not all content that's useful for UI rendering belongs in the model's context |
| 15 | +2. Metadata about content (like priority, intended audience, visual hints) can improve client UX without bloating model context |
| 16 | + |
| 17 | +## Annotation structure |
| 18 | + |
| 19 | +The MCP specification defines the `Annotations` interface with the following structure: |
| 20 | + |
| 21 | +```typescript |
| 22 | +export interface Annotations { |
| 23 | + /** |
| 24 | + * Describes who the intended customer of this object or data is. |
| 25 | + * |
| 26 | + * It can include multiple entries to indicate content useful for multiple audiences (e.g., `["user", "assistant"]`). |
| 27 | + */ |
| 28 | + audience?: Role[]; |
| 29 | + |
| 30 | + /** |
| 31 | + * Describes how important this data is for operating the server. |
| 32 | + * |
| 33 | + * A value of 1 means "most important," and indicates that the data is |
| 34 | + * effectively required, while 0 means "least important," and indicates that |
| 35 | + * the data is entirely optional. |
| 36 | + * |
| 37 | + * @minimum 0 |
| 38 | + * @maximum 1 |
| 39 | + */ |
| 40 | + priority?: number; |
| 41 | +} |
| 42 | +``` |
| 43 | + |
| 44 | +## Where annotations can be used |
| 45 | + |
| 46 | +Annotations can be attached to various MCP objects: |
| 47 | + |
| 48 | +- **Tool results** (`TextContent`, `ImageContent`, `AudioContent`) - helping clients render tool outputs properly without affecting what the model sees |
| 49 | +- **Resources** - providing additional metadata about resources |
| 50 | +- **Resource templates** - adding metadata to resource template definitions |
| 51 | +- **Embedded resources** - enhancing embedded resources with presentation hints |
| 52 | + |
| 53 | +## Example: Tool with annotated results |
| 54 | + |
| 55 | +Here's an example of a tool returning text content with annotations: |
| 56 | + |
| 57 | +```typescript |
| 58 | +// Server-side implementation |
| 59 | +server.setRequestHandler(CallToolRequestSchema, async (request) => { |
| 60 | + if (request.params.name === "weather_forecast") { |
| 61 | + // Tool execution logic... |
| 62 | + return { |
| 63 | + content: [ |
| 64 | + { |
| 65 | + type: "text", |
| 66 | + text: "Tomorrow: Sunny, High 75°F, Low 60°F\nWinds: Light, 5mph NW", |
| 67 | + annotations: { |
| 68 | + audience: ["user"], // This is intended primarily for user viewing |
| 69 | + priority: 1.0 // This is important information |
| 70 | + } |
| 71 | + }, |
| 72 | + { |
| 73 | + type: "text", |
| 74 | + text: "Weather data powered by WeatherAPI.com", |
| 75 | + annotations: { |
| 76 | + audience: ["user"], // This is intended for user viewing only |
| 77 | + priority: 0.2 // This is less important information |
| 78 | + } |
| 79 | + } |
| 80 | + ] |
| 81 | + }; |
| 82 | + } |
| 83 | + throw new Error("Tool not found"); |
| 84 | +}); |
| 85 | +``` |
| 86 | + |
| 87 | +## Tool annotations |
| 88 | + |
| 89 | +Tools have special annotation properties defined in the `ToolAnnotations` interface: |
| 90 | + |
| 91 | +```typescript |
| 92 | +export interface ToolAnnotations { |
| 93 | + /** |
| 94 | + * A human-readable title for the tool. |
| 95 | + */ |
| 96 | + title?: string; |
| 97 | + |
| 98 | + /** |
| 99 | + * If true, the tool does not modify its environment. |
| 100 | + * Default: false |
| 101 | + */ |
| 102 | + readOnlyHint?: boolean; |
| 103 | + |
| 104 | + /** |
| 105 | + * If true, the tool may perform destructive updates to its environment. |
| 106 | + * If false, the tool performs only additive updates. |
| 107 | + * Default: true |
| 108 | + */ |
| 109 | + destructiveHint?: boolean; |
| 110 | + |
| 111 | + /** |
| 112 | + * If true, calling the tool repeatedly with the same arguments |
| 113 | + * will have no additional effect on the its environment. |
| 114 | + * Default: false |
| 115 | + */ |
| 116 | + idempotentHint?: boolean; |
| 117 | + |
| 118 | + /** |
| 119 | + * If true, this tool may interact with an "open world" of external |
| 120 | + * entities. If false, the tool's domain of interaction is closed. |
| 121 | + * Default: true |
| 122 | + */ |
| 123 | + openWorldHint?: boolean; |
| 124 | +} |
| 125 | +``` |
| 126 | + |
| 127 | +Example usage: |
| 128 | + |
| 129 | +```typescript |
| 130 | +{ |
| 131 | + name: "delete_file", |
| 132 | + description: "Delete a file from the filesystem", |
| 133 | + inputSchema: { |
| 134 | + type: "object", |
| 135 | + properties: { |
| 136 | + path: { type: "string" } |
| 137 | + }, |
| 138 | + required: ["path"] |
| 139 | + }, |
| 140 | + annotations: { |
| 141 | + title: "Delete File", |
| 142 | + readOnlyHint: false, |
| 143 | + destructiveHint: true, |
| 144 | + idempotentHint: true, |
| 145 | + openWorldHint: false |
| 146 | + } |
| 147 | +} |
| 148 | +``` |
| 149 | + |
| 150 | +## Best practices |
| 151 | + |
| 152 | +1. **Separate UI concerns from model context** |
| 153 | + - Use annotations for UI-specific information |
| 154 | + - Keep model context focused on necessary information |
| 155 | + |
| 156 | +2. **Be judicious with audience targeting** |
| 157 | + - Use `audience: ["user"]` for content meant only for humans |
| 158 | + - Use `audience: ["assistant"]` for content meant only for the model |
| 159 | + - Use `audience: ["user", "assistant"]` for content relevant to both |
| 160 | + |
| 161 | +3. **Use priority appropriately** |
| 162 | + - Set high priority (close to 1) for essential information |
| 163 | + - Set low priority (close to 0) for supplementary information |
| 164 | + |
| 165 | +4. **Add semantic hints for tools** |
| 166 | + - Use tool annotations to help clients understand the nature of tools |
| 167 | + - Be accurate with hints like `readOnlyHint` and `destructiveHint` |
| 168 | + |
| 169 | +## Security considerations |
| 170 | + |
| 171 | +Tool annotations are just hints and should not be relied upon for security decisions. Clients should never make tool use decisions based solely on annotations received from untrusted servers. |
0 commit comments