Skip to content

Commit b51e9fa

Browse files
yasmewadmtdowlingkstich
authored
Adding doc entry for AI Traits (#2750)
* Add docs for AI Traits Co-authored-by: Michael Dowling <mtdowling@gmail.com> Co-authored-by: Kevin Stich <kevin@kstich.com>
1 parent 426c1ef commit b51e9fa

File tree

2 files changed

+267
-0
lines changed

2 files changed

+267
-0
lines changed
Lines changed: 266 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,266 @@
1+
.. _ai-traits:
2+
3+
================
4+
Smithy AI Traits
5+
================
6+
7+
Smithy AI traits provide metadata for service authors to embed contextual information to guide Large Language Models (LLMs) and other AI systems in understanding and interacting with services.
8+
9+
10+
.. smithy-trait:: smithy.ai#prompts
11+
.. _smithy.ai#prompts-trait:
12+
13+
---------------------------
14+
``smithy.ai#prompts`` trait
15+
---------------------------
16+
17+
Summary
18+
Defines prompt templates that provide contextual guidance to LLMs for understanding when and how to use operations or services. This trait can be used to generate prompts for a Model Context Protocol (MCP) server.
19+
Trait selector
20+
``:is(service, operation)``
21+
Value type
22+
``map`` where keys are prompt names and values are prompt template definitions.
23+
24+
The ``smithy.ai#prompts`` trait allows service authors to provide structured guidance to AI systems, including contextual descriptions, parameter templates with placeholder syntax, usage conditions, and workflow guidance for complex processes.
25+
26+
Each prompt template definition consists of the following components:
27+
28+
.. list-table::
29+
:header-rows: 1
30+
:widths: 15 15 70
31+
32+
* - Property
33+
- Type
34+
- Description
35+
* - description
36+
- ``string``
37+
- **Required.** A concise description of the prompt's purpose and functionality.
38+
* - template
39+
- ``string``
40+
- **Required.** The prompt template text.
41+
* - arguments
42+
- ``string``
43+
- Optional reference to a structure shape that defines the parameters used in the template placeholders. Valid value MUST be a structure shapeId.
44+
* - preferWhen
45+
- ``string``
46+
- Optional condition to provide preference for tool selection.May be used for routing to other tools or prompts.
47+
48+
Template Placeholder Syntax
49+
===========================
50+
51+
Prompt templates use ``{{parameterName}}`` syntax to reference parameters from the associated ``arguments`` structure. This can be useful to reference and interpolate members of arguments when accepting user input for prompts in Model Context Protocol (MCP) servers.
52+
53+
.. code-block:: smithy
54+
55+
@prompts({
56+
detailed_weather_report: {
57+
description: "Generate a comprehensive weather report"
58+
template: "Create a detailed weather report for {{location}} showing {{temperature}}°F with {{conditions}}. Include humidity at {{humidity}}% and provide recommendations for outdoor activities."
59+
arguments: DetailedWeatherInput
60+
}
61+
})
62+
service WeatherService {
63+
operations: [GetCurrentWeather]
64+
}
65+
66+
operation GetCurrentWeather {
67+
input := {
68+
location: String
69+
}
70+
71+
output := {
72+
temperature : String
73+
}
74+
}
75+
76+
structure DetailedWeatherInput {
77+
location: String
78+
temperature: Float
79+
conditions: String
80+
humidity: Float
81+
}
82+
83+
Service-level vs operation-level prompts
84+
========================================
85+
86+
**Service-level prompts** SHOULD be used to define workflows that compose multiple operations, provide guidance about the service and offer prompts that make complex interactions with the service simple:
87+
88+
.. code-block:: smithy
89+
90+
@prompts({
91+
service_overview: {
92+
description: "Overview of weather service capabilities"
93+
template: "This weather service provides current conditions and forecasts. Use GetCurrentWeather for immediate conditions, and leverage creative prompts like emoji_weather for enhanced user experience."
94+
preferWhen: "User needs to understand available weather service features"
95+
},
96+
vacation_weather_planner: {
97+
description: "Plan weather for vacation destinations"
98+
template: "Help plan a vacation by comparing weather between {{homeLocation}} and {{destinationLocation}}.
99+
Call GetCurrentWeather for both locations, then provide a recommendation on the best time to travel, what to pack, and activities to consider based on weather differences."
100+
arguments: VacationPlannerInput
101+
}
102+
})
103+
service WeatherService {
104+
operations: [GetCurrentWeather]
105+
}
106+
107+
**Operation-level prompts** SHOULD provide specific guidance for individual operations and their usage:
108+
109+
.. code-block:: smithy
110+
111+
@prompts({
112+
emergency_weather_check: {
113+
description: "Quick weather check for emergency situations"
114+
template: "Immediately check weather conditions at {{location}} focusing on safety concerns. Prioritize severe weather alerts, visibility, and hazardous conditions."
115+
arguments: GetCurrentWeatherInput
116+
preferWhen: "Emergency situations requiring immediate weather assessment"
117+
}
118+
})
119+
operation GetCurrentWeather {
120+
input: GetCurrentWeatherInput
121+
output: GetCurrentWeatherOutput
122+
}
123+
124+
structure GetCurrentWeatherInput {
125+
/// Location for getting weather
126+
@required
127+
location: String
128+
}
129+
130+
Use cases for prompts
131+
=====================
132+
133+
Service authors can define sophisticated workflows by defining prompts to orchestrate multiple API calls and format results.
134+
135+
**Output formatting**
136+
137+
Service teams can control how LLMs present API results:
138+
139+
.. code-block:: smithy
140+
141+
@prompts({
142+
weather_dashboard: {
143+
description: "Create a weather dashboard with multiple data points"
144+
template: "Get weather for {{location}} and create a dashboard view. Format as: 🌡️ Temperature: [temp]°F | 💧 Humidity: [humidity]% | 🌤️ Conditions: [conditions]. Add appropriate weather emoji and brief activity recommendations."
145+
arguments: GetCurrentWeatherInput
146+
}
147+
})
148+
operation GetWeather {
149+
input := {
150+
location: String
151+
}
152+
153+
output := {
154+
temperature : String
155+
}
156+
}
157+
158+
**Multi-operation workflows**
159+
160+
Prompts can guide LLMs to perform complex workflows using existing operations:
161+
162+
.. code-block:: smithy
163+
164+
@prompts({
165+
vacation_weather_planner: {
166+
description: "Plan weather for vacation destinations"
167+
template: "Help plan a vacation by comparing weather between {{homeLocation}} and {{destinationLocation}}. Call GetCurrentWeather for both locations, then provide a recommendation on the best time to travel, what to pack, and activities to consider based on weather differences."
168+
arguments: VacationPlannerInput
169+
}
170+
})
171+
operation GetWeather {
172+
input := {
173+
location: String
174+
}
175+
176+
output := {
177+
temperature : String
178+
}
179+
}
180+
181+
structure VacationPlannerInput {
182+
@required
183+
homeLocation: String
184+
@required
185+
destinationLocation: String
186+
}
187+
188+
A complete example
189+
==================
190+
191+
The following example demonstrates creative prompt templates that enhance the user experience with a weather service:
192+
193+
.. code-block:: smithy
194+
195+
$version: "2"
196+
197+
namespace example.weather
198+
199+
use smithy.ai#prompts
200+
201+
@prompts({
202+
emoji_weather: {
203+
description: "Get weather with emoji visualization"
204+
template: "Get current weather for {{location}} and display it with appropriate weather emojis (☀️ sunny, ⛅ partly cloudy, ☁️ cloudy, 🌧️ rainy, ⛈️ stormy, ❄️ snowy)."
205+
arguments: GetCurrentWeatherInput
206+
preferWhen: "User wants a fun, visual weather display"
207+
}
208+
travel_weather_advisor: {
209+
description: "Provides complete travel guidance according to the weather"
210+
template: "Get weather for {{location}} and provide travel advice. Include clothing recommendations based on temperature and conditions, suggest appropriate activities, and highlight any weather concerns."
211+
arguments: GetCurrentWeatherInput
212+
preferWhen: "User is planning travel or outdoor activities"
213+
}
214+
weather_comparison: {
215+
description: "Compare weather between multiple locations"
216+
template: "Compare current weather between {{location1}} and {{location2}}. Call GetCurrentWeather for each location, then present results in a table format showing temperature, conditions, and humidity. Highlight which location has better weather conditions."
217+
arguments: WeatherComparisonInput
218+
preferWhen: "User wants to compare weather across different cities"
219+
}
220+
})
221+
service WeatherService {
222+
version: "2024-01-01"
223+
operations: [GetCurrentWeather]
224+
}
225+
226+
operation GetCurrentWeather {
227+
input: GetCurrentWeatherInput
228+
output: GetCurrentWeatherOutput
229+
}
230+
231+
structure GetCurrentWeatherInput {
232+
/// Location to get weather for (city, coordinates, or address)
233+
@required
234+
location: String
235+
}
236+
237+
structure GetCurrentWeatherOutput {
238+
temperature: Float
239+
humidity: Float
240+
conditions: String
241+
}
242+
243+
structure WeatherComparisonInput {
244+
/// First location to compare
245+
@required
246+
location1: String
247+
248+
/// Second location to compare
249+
@required
250+
location2: String
251+
}
252+
253+
Integration with Model Context Protocol
254+
=======================================
255+
256+
The ``smithy.ai#prompts`` trait is designed to work with Model Context Protocol (MCP) servers. MCP servers can use the metadata defined in the trait to generate `prompts`_ as defined in the Model Context Protocol (MCP) specification.
257+
258+
See also
259+
- `MCP Server Example`_ for a complete implementation of an MCP server using Smithy
260+
- `MCP Traits Example`_ for additional examples of AI traits in practice
261+
- `MCP Server Model`_ for the Smithy model definition used in the MCP server example
262+
263+
.. _MCP Server Example: https://github.com/smithy-lang/smithy-java/tree/main/examples/mcp-server
264+
.. _MCP Traits Example: https://github.com/smithy-lang/smithy-java/tree/main/examples/mcp-traits-example
265+
.. _MCP Server Model: https://github.com/smithy-lang/smithy-java/blob/main/examples/mcp-server/src/main/resources/software/amazon/smithy/java/example/server/mcp/main.smithy
266+
.. _Prompts: https://modelcontextprotocol.io/specification/2025-06-18/server/prompts

docs/source-2.0/additional-specs/index.rst

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@ start with ``smithy.*`` where "*" is anything other than ``api``.
1010
:maxdepth: 1
1111
:caption: smithy.* specifications
1212

13+
ai-traits
1314
http-protocol-compliance-tests
1415
smoke-tests
1516
waiters

0 commit comments

Comments
 (0)