|
| 1 | +# MCP Traits Example |
| 2 | + |
| 3 | +This example demonstrates how to use the `@prompts` trait from the `mcp-traits` module to provide LLM guidance for Smithy services. The `@prompts` trait allows you to annotate services, resources, and operations with prompt templates that help LLMs understand when and how to use your API. |
| 4 | + |
| 5 | +## Overview |
| 6 | + |
| 7 | +The `@prompts` trait enables you to: |
| 8 | + |
| 9 | +- **Provide contextual guidance**: Help LLMs understand when to use specific operations |
| 10 | +- **Include parameter descriptions**: Guide LLMs on how to populate operation inputs |
| 11 | +- **Define usage conditions**: Specify when certain operations should be preferred |
| 12 | +- **Create comprehensive templates**: Provide detailed instructions and examples |
| 13 | + |
| 14 | +## Example Implementation |
| 15 | + |
| 16 | +This example implements a simple `UserService` that demonstrates the key features of the `@prompts` trait: |
| 17 | + |
| 18 | +### Service-Level Prompts |
| 19 | + |
| 20 | +```smithy |
| 21 | +@prompts({ |
| 22 | + search_users: { |
| 23 | + description: "Search for users in the system by various criteria", |
| 24 | + template: "Search for users where {{searchCriteria}}. Use pagination with limit={{limit}} if many results expected.", |
| 25 | + arguments: SearchUsersInput, |
| 26 | + preferWhen: "User wants to find specific users or browse user lists" |
| 27 | + } |
| 28 | +}) |
| 29 | +service UserService { |
| 30 | + operations: [ |
| 31 | + SearchUsers |
| 32 | + ] |
| 33 | +} |
| 34 | +``` |
| 35 | + |
| 36 | +### Key Features |
| 37 | + |
| 38 | +1. **Service-Level Guidance**: The service includes a prompt that explains how to search for users |
| 39 | +2. **Parameter Templates**: Uses `{{searchCriteria}}` and `{{limit}}` placeholders that reference the input structure |
| 40 | +3. **ArgumentShape Integration**: References `SearchUsersInput` to provide parameter context |
| 41 | +4. **Conditional Usage**: Uses `preferWhen` to specify when this operation should be preferred |
| 42 | + |
| 43 | +### Complete Model Structure |
| 44 | + |
| 45 | +```smithy |
| 46 | +namespace com.example |
| 47 | +
|
| 48 | +use amazon.smithy.llm#prompts |
| 49 | +
|
| 50 | +@prompts({ |
| 51 | + search_users: { |
| 52 | + description: "Search for users in the system by various criteria", |
| 53 | + template: "Search for users where {{searchCriteria}}. Use pagination with limit={{limit}} if many results expected.", |
| 54 | + arguments: SearchUsersInput, |
| 55 | + preferWhen: "User wants to find specific users or browse user lists" |
| 56 | + } |
| 57 | +}) |
| 58 | +service UserService { |
| 59 | + operations: [ |
| 60 | + SearchUsers |
| 61 | + ] |
| 62 | +} |
| 63 | +
|
| 64 | +operation SearchUsers { |
| 65 | + input: SearchUsersInput |
| 66 | + output: SearchUsersOutput |
| 67 | +} |
| 68 | +
|
| 69 | +structure SearchUsersInput { |
| 70 | + searchCriteria: String |
| 71 | + limit: Integer |
| 72 | +} |
| 73 | +
|
| 74 | +structure SearchUsersOutput { |
| 75 | + id: String |
| 76 | +} |
| 77 | +``` |
| 78 | + |
| 79 | +## Prompt Template Structure |
| 80 | + |
| 81 | +Each prompt template in the `@prompts` trait map has the following structure: |
| 82 | + |
| 83 | +```smithy |
| 84 | +"prompt_name": { |
| 85 | + description: String // Required: Brief description of the prompt's purpose |
| 86 | + template: String // Required: The actual prompt template with placeholders |
| 87 | + arguments: ArgumentShape // Optional: Reference to structure defining parameters |
| 88 | + preferWhen: String // Optional: Condition describing when to use this prompt |
| 89 | +} |
| 90 | +``` |
| 91 | + |
| 92 | +### Template Placeholders |
| 93 | + |
| 94 | +Use `{{parameterName}}` syntax in templates to reference parameters defined in the `arguments` structure: |
| 95 | + |
| 96 | +```smithy |
| 97 | +template: "Search for users where {{searchCriteria}}. Use pagination with limit={{limit}} if many results expected." |
| 98 | +arguments: SearchUsersInput |
| 99 | +``` |
| 100 | + |
| 101 | +The placeholders `{{searchCriteria}}` and `{{limit}}` correspond to the fields in the `SearchUsersInput` structure. |
| 102 | + |
| 103 | +### Conditional Usage |
| 104 | + |
| 105 | +The `preferWhen` field helps LLMs decide when to use specific operations: |
| 106 | + |
| 107 | +```smithy |
| 108 | +preferWhen: "User wants to find specific users or browse user lists" |
| 109 | +``` |
| 110 | + |
| 111 | +## Building and Running |
| 112 | + |
| 113 | +### Prerequisites |
| 114 | + |
| 115 | +- Java 17 or later |
| 116 | +- Gradle 7.0 or later |
| 117 | + |
| 118 | +### Build the Example |
| 119 | + |
| 120 | +```bash |
| 121 | +# From the example directory |
| 122 | +gradle build |
| 123 | +``` |
| 124 | + |
| 125 | +### Validate the Model |
| 126 | + |
| 127 | +```bash |
| 128 | +# Run Smithy build to validate the model and traits |
| 129 | +gradle smithyBuild |
| 130 | +``` |
| 131 | + |
| 132 | +### Expected Output |
| 133 | + |
| 134 | +When you run `gradle smithyBuild`, you should see: |
| 135 | + |
| 136 | +1. **Successful build** - indicating all traits are applied correctly |
| 137 | +2. **No validation errors** - confirming the model structure is valid |
| 138 | +3. **Generated build artifacts** - in the `build/smithyprojections/` directory |
| 139 | + |
| 140 | +## Usage Patterns |
| 141 | + |
| 142 | +### 1. Service-Level Guidance |
| 143 | + |
| 144 | +Provide high-level guidance about the entire service: |
| 145 | + |
| 146 | +```smithy |
| 147 | +@prompts({ |
| 148 | + service_overview: { |
| 149 | + description: "Overview of the user management service" |
| 150 | + template: "This service manages user accounts and provides search capabilities." |
| 151 | + preferWhen: "User needs general information about user management" |
| 152 | + } |
| 153 | +}) |
| 154 | +service UserService { |
| 155 | + // ... |
| 156 | +} |
| 157 | +``` |
| 158 | + |
| 159 | +### 2. Parameter-Rich Templates |
| 160 | + |
| 161 | +Create detailed templates that guide parameter usage: |
| 162 | + |
| 163 | +```smithy |
| 164 | +template: "Search for users where {{searchCriteria}}. Use pagination with limit={{limit}} if many results expected." |
| 165 | +``` |
| 166 | + |
| 167 | +### 3. Contextual Guidance |
| 168 | + |
| 169 | +Use `preferWhen` to provide context about when operations should be used: |
| 170 | + |
| 171 | +```smithy |
| 172 | +preferWhen: "User wants to find specific users or browse user lists" |
| 173 | +``` |
| 174 | + |
| 175 | +## Best Practices |
| 176 | + |
| 177 | +### 1. Clear Descriptions |
| 178 | + |
| 179 | +- Use concise but descriptive prompt names |
| 180 | +- Write clear `description` fields that explain the prompt's purpose |
| 181 | +- Make templates self-contained and easy to understand |
| 182 | + |
| 183 | +### 2. Comprehensive Templates |
| 184 | + |
| 185 | +- Include all relevant parameters in templates using `{{parameterName}}` syntax |
| 186 | +- Provide examples and usage patterns within templates |
| 187 | +- Explain constraints and expected input formats |
| 188 | + |
| 189 | +### 3. Meaningful Parameter References |
| 190 | + |
| 191 | +- Use the `arguments` field to reference input structures |
| 192 | +- Ensure parameter names in templates match structure field names |
| 193 | +- Keep parameter names descriptive and intuitive |
| 194 | + |
| 195 | +### 4. Conditional Logic |
| 196 | + |
| 197 | +- Use `preferWhen` to guide operation selection |
| 198 | +- Be specific about when operations should be used |
| 199 | +- Consider user intent and context in conditions |
| 200 | + |
| 201 | +## Integration with MCP |
| 202 | + |
| 203 | +This example is designed to work with Model Context Protocol (MCP) servers. The `@prompts` trait provides the metadata needed for MCP servers to: |
| 204 | + |
| 205 | +1. **Understand API capabilities** - through service-level prompts |
| 206 | +2. **Generate appropriate calls** - using operation-specific guidance |
| 207 | +3. **Populate parameters correctly** - through ArgumentShape references |
| 208 | +4. **Provide context-aware suggestions** - using `preferWhen` conditions |
| 209 | + |
| 210 | +## Extending the Example |
| 211 | + |
| 212 | +To extend this example, you could: |
| 213 | + |
| 214 | +1. **Add more operations** with their own prompt templates |
| 215 | +2. **Include resource-level prompts** for hierarchical guidance |
| 216 | +3. **Add error handling prompts** for common failure scenarios |
| 217 | +4. **Create workflow prompts** that guide multi-step processes |
| 218 | + |
| 219 | +## Further Reading |
| 220 | + |
| 221 | +- [Smithy Trait Documentation](https://smithy.io/2.0/spec/traits.html) |
| 222 | +- [Model Context Protocol](https://modelcontextprotocol.io/) |
| 223 | +- [Smithy Java Documentation](https://smithy.io/2.0/languages/java/) |
| 224 | + |
| 225 | +## License |
| 226 | + |
| 227 | +This example is provided under the same license as the Smithy Java project. |
0 commit comments