Skip to content

Commit f8618ba

Browse files
committed
reorg sections for java; add code snippet
1 parent 38b1303 commit f8618ba

1 file changed

Lines changed: 54 additions & 29 deletions

File tree

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

Lines changed: 54 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -299,8 +299,7 @@ For the complete code example, see [function_app.py](https://github.com/Azure-Sa
299299
::: zone-end
300300
301301
::: zone pivot="programming-language-java"
302-
303-
The following example shows a Java function that serves an HTML weather widget as an MCP resource:
302+
The following code creates an endpoint to expose 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.
304303
305304
```java
306305
@FunctionName("GetWeatherWidget")
@@ -321,28 +320,67 @@ public String getWeatherWidget(
321320
executionContext.getLogger().info("GetWeatherWidget: serving weather widget UI");
322321
323322
java.io.File file = new java.io.File("app/dist/index.html");
324-
if (file.exists()) {
325-
try {
326-
return java.nio.file.Files.readString(file.toPath(), StandardCharsets.UTF_8);
327-
} catch (IOException e) {
328-
executionContext.getLogger().log(Level.WARNING, "Failed to read UI from file", e);
329-
}
330-
}
331-
332-
return "<html><body><p>Weather widget UI not found.</p></body></html>";
323+
return java.nio.file.Files.readString(file.toPath(), StandardCharsets.UTF_8);
333324
}
334325
```
335326
336327
The `RESOURCE_METADATA` constant is defined as a JSON string that provides additional MCP metadata surfaced via the protocol's `_meta` field:
337328

338329
```java
339330
private static final String RESOURCE_METADATA = """
340-
{
341-
"ui": {
342-
"prefersBorder": true
343-
}
331+
{
332+
"ui": {
333+
"prefersBorder": true
334+
}
335+
}
336+
""";
337+
```
338+
339+
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:
340+
341+
```java
342+
343+
private static final String TOOL_METADATA = """
344+
{
345+
"ui": {
346+
"resourceUri": "ui://weather/index.html"
347+
}
348+
}
349+
""";
350+
351+
@FunctionName("GetWeather")
352+
public String getWeather(
353+
@McpToolTrigger(
354+
name = "GetWeather",
355+
description = "Returns current weather for a location via Open-Meteo.")
356+
@McpMetadata(
357+
name = "GetWeather",
358+
json = TOOL_METADATA)
359+
String context,
360+
@McpToolProperty(
361+
name = "location",
362+
propertyType = "string",
363+
description = "City name to check weather for (e.g., Seattle, New York, Miami)")
364+
String location,
365+
final ExecutionContext executionContext) {
366+
Object result = weatherService.getCurrentWeather(location);
367+
return MAPPER.writeValueAsString(result);
344368
}
345-
""";
369+
```
370+
371+
372+
For the complete code example, see [McpWeatherApp](https://github.com/Azure-Samples/remote-mcp-functions-java/tree/main/samples/McpWeatherApp/src/main/java/com/function/weather).
373+
374+
### Dependency
375+
376+
Add the following dependency to your project's `pom.xml` file. The MCP annotations are included in the Azure Functions Java library starting from version **3.2.4**:
377+
378+
```xml name=pom.xml
379+
<dependency>
380+
<groupId>com.microsoft.azure.functions</groupId>
381+
<artifactId>azure-functions-java-library</artifactId>
382+
<version>3.2.4</version>
383+
</dependency>
346384
```
347385
::: zone-end
348386
@@ -457,19 +495,6 @@ The `@McpMetadata` annotation supports the following properties:
457495
@McpMetadata(
458496
name = "context",
459497
json = "{\"author\": \"John Doe\", \"version\": 1.0}")
460-
String context
461-
```
462-
463-
## Setup
464-
465-
Add the following dependency to your project's `pom.xml` file. The MCP annotations are included in the Azure Functions Java library starting from version **3.2.4**:
466-
467-
```xml name=pom.xml
468-
<dependency>
469-
<groupId>com.microsoft.azure.functions</groupId>
470-
<artifactId>azure-functions-java-library</artifactId>
471-
<version>3.2.4</version>
472-
</dependency>
473498
```
474499
::: zone-end
475500

0 commit comments

Comments
 (0)