Skip to content

Commit 1185090

Browse files
committed
add missing code snippet
1 parent b335b07 commit 1185090

1 file changed

Lines changed: 72 additions & 0 deletions

File tree

articles/azure-functions/functions-bindings-mcp-resource-trigger.md

Lines changed: 72 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -280,6 +280,78 @@ For the complete code example, see [function_app.py](https://github.com/Azure-Sa
280280
281281
::: zone-end
282282

283+
::: zone pivot="programming-language-java"
284+
The following code registers a resource named `Weather Widget` that serves an interactive weather display as bundled HTML content. The resource uses the `ui://` scheme to indicate it's an MCP App UI resource.
285+
286+
```java
287+
private static final String RESOURCE_METADATA = """
288+
{
289+
"ui": {
290+
"prefersBorder": true
291+
}
292+
}
293+
""";
294+
295+
@FunctionName("GetWeatherWidget")
296+
public String getWeatherWidget(
297+
@McpResourceTrigger(
298+
name = "context",
299+
uri = "ui://weather/index.html",
300+
resourceName = "Weather Widget",
301+
title = "Weather Widget",
302+
description = "Interactive weather display for MCP Apps",
303+
mimeType = "text/html;profile=mcp-app")
304+
@McpMetadata(
305+
name = "context",
306+
json = RESOURCE_METADATA)
307+
String context,
308+
final ExecutionContext executionContext) {
309+
310+
executionContext.getLogger().info("GetWeatherWidget: serving weather widget UI");
311+
312+
return java.nio.file.Files.readString(file.toPath(), StandardCharsets.UTF_8);
313+
}
314+
```
315+
316+
A tool can reference this resource by declaring a `resourceUri` in its metadata, pointing to `ui://weather/index.html`. When the tool is invoked, the MCP host fetches the resource and renders it:
317+
318+
```java
319+
private static final String TOOL_METADATA = """
320+
{
321+
"ui": {
322+
"resourceUri": "ui://weather/index.html"
323+
}
324+
}
325+
""";
326+
327+
@FunctionName("GetWeather")
328+
public String getWeather(
329+
@McpToolTrigger(
330+
name = "GetWeather",
331+
description = "Returns current weather for a location via Open-Meteo.")
332+
@McpMetadata(
333+
name = "GetWeather",
334+
json = TOOL_METADATA)
335+
String context,
336+
@McpToolProperty(
337+
name = "location",
338+
propertyType = "string",
339+
description = "City name to check weather for (e.g., Seattle, New York, Miami)")
340+
String location,
341+
final ExecutionContext executionContext) {
342+
343+
executionContext.getLogger().info("GetWeather: looking up weather for '" + location + "'");
344+
345+
Object result = weatherService.getCurrentWeather(location);
346+
347+
return MAPPER.writeValueAsString(result);
348+
}
349+
```
350+
351+
For the complete code example, see [WeatherFunction.java](https://github.com/Azure-Samples/remote-mcp-functions-java/blob/main/samples/McpWeatherApp/src/main/java/com/function/weather/WeatherFunction.java).
352+
353+
::: zone-end
354+
283355
[!INCLUDE [functions-mcp-extension-powershell-note](../../includes/functions-mcp-extension-powershell-note.md)]
284356
285357
::: zone pivot="programming-language-csharp"

0 commit comments

Comments
 (0)