Skip to content

Commit fab86de

Browse files
Merge pull request #314345 from lilyjma/java-mcp-resource-ref
Add Java MCP resource trigger reference doc
2 parents 133d9f4 + 1185090 commit fab86de

1 file changed

Lines changed: 145 additions & 11 deletions

File tree

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

Lines changed: 145 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@ For information on setup and configuration details, see the [overview](functions
2222
> For C#, the Azure Functions MCP extension supports only the [isolated worker model](dotnet-isolated-process-guide.md).
2323
::: zone-end
2424

25-
::: zone pivot="programming-language-csharp,programming-language-python,programming-language-typescript,programming-language-javascript"
25+
::: zone pivot="programming-language-csharp,programming-language-python,programming-language-typescript,programming-language-javascript, programming-language-java"
2626

2727
This first example shows how to use resource to implement the UI element of MCP Apps.
2828
::: zone-end
@@ -127,12 +127,6 @@ For the complete code example, see the [Azure Functions MCP Extension repo](http
127127

128128
::: zone-end
129129

130-
::: zone pivot="programming-language-java"
131-
132-
> [!IMPORTANT]
133-
> The MCP extension in Java supports resource today. Documentation coming soon.
134-
135-
::: zone-end
136130

137131
::: zone pivot="programming-language-javascript"
138132
Example code for JavaScript isn't currently available. See the TypeScript example for general guidance.
@@ -286,6 +280,78 @@ For the complete code example, see [function_app.py](https://github.com/Azure-Sa
286280
287281
::: zone-end
288282

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+
289355
[!INCLUDE [functions-mcp-extension-powershell-note](../../includes/functions-mcp-extension-powershell-note.md)]
290356
291357
::: zone pivot="programming-language-csharp"
@@ -355,7 +421,52 @@ Define the trigger's binding options in your code. The trigger supports the foll
355421
356422
::: zone-end
357423
358-
::: zone pivot="programming-language-csharp,programming-language-python,programming-language-typescript"
424+
::: zone pivot="programming-language-java"
425+
426+
## Attributes
427+
428+
Apply the `@McpResourceTrigger` annotation to a function parameter to define an MCP resource trigger.
429+
430+
The `@McpResourceTrigger` annotation supports the following properties:
431+
432+
| Property | Description |
433+
|---|---|
434+
| **`name`** | Required. The binding name for the resource invocation context parameter. |
435+
| **`uri`** | Required. The URI of the MCP resource (for example, `"file://readme.md"` or `"ui://weather/index.html"`). |
436+
| **`resourceName`** | Required. The display name of the MCP resource. |
437+
| **`title`** | Optional. A human-readable title for display purposes. Unlike `resourceName`, which is a programmatic identifier, this is a friendly label for UI presentation. |
438+
| **`description`** | Optional. A human-readable description of this resource. |
439+
| **`mimeType`** | Optional. The MIME type of the resource content (for example, `"text/plain"`, `"text/html"`, `"image/png"`, `"text/html;profile=mcp-app"`). |
440+
| **`size`** | Optional. The size of the resource in bytes. Defaults to `-1` (not specified). |
441+
| **`dataType`** | Optional. Defines how the Functions runtime should treat the parameter value. Possible values: `""` (default, deserialize to parameter type), `"string"`, `"binary"`. |
442+
443+
## Metadata annotation
444+
445+
You can optionally apply `@McpMetadata` on the same parameter as `@McpResourceTrigger` to attach arbitrary JSON metadata to the resource. This metadata is surfaced in the MCP protocol's `_meta` field when clients call `resources/list`.
446+
447+
The `@McpMetadata` annotation supports the following properties:
448+
449+
| Property | Description |
450+
|---|---|
451+
| **`name`** | Required. The binding parameter name. Should match the `name` value of the trigger annotation on the same parameter. |
452+
| **`json`** | Required. The metadata as a valid JSON string. Can include any arbitrary key-value pairs such as author information, version numbers, UI hints, or tags. |
453+
454+
**Example:**
455+
456+
```java
457+
@McpResourceTrigger(
458+
name = "context",
459+
uri = "file://readme.md",
460+
resourceName = "readme",
461+
description = "Application readme file",
462+
mimeType = "text/plain")
463+
@McpMetadata(
464+
name = "context",
465+
json = "{\"author\": \"John Doe\", \"version\": 1.0}")
466+
```
467+
::: zone-end
468+
469+
::: zone pivot="programming-language-csharp,programming-language-java,programming-language-python,programming-language-typescript"
359470
360471
See the [Example section](#example) for complete examples.
361472
@@ -400,7 +511,13 @@ The resource handler function has two parameters:
400511
401512
::: zone-end
402513
403-
::: zone pivot="programming-language-csharp,programming-language-python,programming-language-javascript,programming-language-typescript"
514+
::: zone pivot="programming-language-java"
515+
516+
The MCP resource trigger binds the resource invocation context to a function parameter. The trigger can bind to the following types: `String`, or `byte[]` for binary content.
517+
518+
::: zone-end
519+
520+
::: zone pivot="programming-language-csharp,programming-language-java,programming-language-python,programming-language-javascript,programming-language-typescript"
404521
405522
### Resource URIs
406523
@@ -428,7 +545,13 @@ Use the `metadata` option to provide extra metadata for resources. This metadata
428545
429546
::: zone-end
430547
431-
::: zone pivot="programming-language-csharp,programming-language-python,programming-language-javascript,programming-language-typescript"
548+
::: zone pivot="programming-language-java"
549+
550+
Use the `@McpMetadata` annotation to provide extra metadata for resources. This metadata is a JSON-serialized string included in the `meta` field of each resource when clients call `resources/list`. It can affect how the resource content is displayed or processed.
551+
552+
::: zone-end
553+
554+
::: zone pivot="programming-language-csharp,programming-language-java,programming-language-python,programming-language-javascript,programming-language-typescript"
432555
433556
### Return types
434557
@@ -462,7 +585,18 @@ The function should return a `string` containing the resource content (for examp
462585
463586
::: zone-end
464587
465-
::: zone pivot="programming-language-csharp,programming-language-python,programming-language-javascript,programming-language-typescript"
588+
::: zone pivot="programming-language-java"
589+
590+
The MCP resource trigger supports the following return types:
591+
592+
| Type | Description |
593+
| --- | --- |
594+
| `String` | Returned as text content in the MCP `ReadResourceResult`. |
595+
| `byte[]` | Returned as base64-encoded binary content in the MCP `ReadResourceResult`. Set `dataType = "binary"` on the annotation when returning binary content. |
596+
597+
::: zone-end
598+
599+
::: zone pivot="programming-language-csharp,programming-language-java,programming-language-python,programming-language-javascript,programming-language-typescript"
466600
467601
### Resource discovery
468602

0 commit comments

Comments
 (0)