Skip to content

Commit 38b1303

Browse files
committed
add Java resource reference doc
1 parent 631b9e1 commit 38b1303

1 file changed

Lines changed: 134 additions & 11 deletions

File tree

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

Lines changed: 134 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 doesn't_ support resource today.
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.
@@ -304,6 +298,54 @@ For the complete code example, see [function_app.py](https://github.com/Azure-Sa
304298
305299
::: zone-end
306300
301+
::: zone pivot="programming-language-java"
302+
303+
The following example shows a Java function that serves an HTML weather widget as an MCP resource:
304+
305+
```java
306+
@FunctionName("GetWeatherWidget")
307+
public String getWeatherWidget(
308+
@McpResourceTrigger(
309+
name = "context",
310+
uri = "ui://weather/index.html",
311+
resourceName = "Weather Widget",
312+
title = "Weather Widget",
313+
description = "Interactive weather display for MCP Apps",
314+
mimeType = "text/html;profile=mcp-app")
315+
@McpMetadata(
316+
name = "context",
317+
json = RESOURCE_METADATA)
318+
String context,
319+
final ExecutionContext executionContext) {
320+
321+
executionContext.getLogger().info("GetWeatherWidget: serving weather widget UI");
322+
323+
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>";
333+
}
334+
```
335+
336+
The `RESOURCE_METADATA` constant is defined as a JSON string that provides additional MCP metadata surfaced via the protocol's `_meta` field:
337+
338+
```java
339+
private static final String RESOURCE_METADATA = """
340+
{
341+
"ui": {
342+
"prefersBorder": true
343+
}
344+
}
345+
""";
346+
```
347+
::: zone-end
348+
307349
[!INCLUDE [functions-mcp-extension-powershell-note](../../includes/functions-mcp-extension-powershell-note.md)]
308350

309351
::: zone pivot="programming-language-csharp"
@@ -373,7 +415,65 @@ Define the trigger's binding options in your code. The trigger supports the foll
373415

374416
::: zone-end
375417

376-
::: zone pivot="programming-language-csharp,programming-language-python,programming-language-typescript"
418+
::: zone pivot="programming-language-java"
419+
420+
## Attributes
421+
422+
Apply the `@McpResourceTrigger` annotation to a function parameter to define an MCP resource trigger.
423+
424+
The `@McpResourceTrigger` annotation supports the following properties:
425+
426+
| Property | Description |
427+
|---|---|
428+
| **`name`** | Required. The binding name for the resource invocation context parameter. |
429+
| **`uri`** | Required. The URI of the MCP resource (for example, `"file://readme.md"` or `"ui://weather/index.html"`). |
430+
| **`resourceName`** | Required. The display name of the MCP resource. |
431+
| **`title`** | Optional. A human-readable title for display purposes. Unlike `resourceName`, which is a programmatic identifier, this is a friendly label for UI presentation. |
432+
| **`description`** | Optional. A human-readable description of this resource. |
433+
| **`mimeType`** | Optional. The MIME type of the resource content (for example, `"text/plain"`, `"text/html"`, `"image/png"`, `"text/html;profile=mcp-app"`). |
434+
| **`size`** | Optional. The size of the resource in bytes. Defaults to `-1` (not specified). |
435+
| **`dataType`** | Optional. Defines how the Functions runtime should treat the parameter value. Possible values: `""` (default, deserialize to parameter type), `"string"`, `"binary"`. |
436+
437+
## Metadata annotation
438+
439+
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`.
440+
441+
The `@McpMetadata` annotation supports the following properties:
442+
443+
| Property | Description |
444+
|---|---|
445+
| **`name`** | Required. The binding parameter name. Should match the `name` value of the trigger annotation on the same parameter. |
446+
| **`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. |
447+
448+
**Example:**
449+
450+
```java
451+
@McpResourceTrigger(
452+
name = "context",
453+
uri = "file://readme.md",
454+
resourceName = "readme",
455+
description = "Application readme file",
456+
mimeType = "text/plain")
457+
@McpMetadata(
458+
name = "context",
459+
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>
473+
```
474+
::: zone-end
475+
476+
::: zone pivot="programming-language-csharp,programming-language-java,programming-language-python,programming-language-typescript"
377477

378478
See the [Example section](#example) for complete examples.
379479

@@ -418,7 +518,13 @@ The resource handler function has two parameters:
418518

419519
::: zone-end
420520

421-
::: zone pivot="programming-language-csharp,programming-language-python,programming-language-javascript,programming-language-typescript"
521+
::: zone pivot="programming-language-java"
522+
523+
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.
524+
525+
::: zone-end
526+
527+
::: zone pivot="programming-language-csharp,programming-language-java,programming-language-python,programming-language-javascript,programming-language-typescript"
422528

423529
### Resource URIs
424530

@@ -446,7 +552,13 @@ Use the `metadata` option to provide extra metadata for resources. This metadata
446552

447553
::: zone-end
448554

449-
::: zone pivot="programming-language-csharp,programming-language-python,programming-language-javascript,programming-language-typescript"
555+
::: zone pivot="programming-language-java"
556+
557+
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.
558+
559+
::: zone-end
560+
561+
::: zone pivot="programming-language-csharp,programming-language-java,programming-language-python,programming-language-javascript,programming-language-typescript"
450562

451563
### Return types
452564

@@ -480,7 +592,18 @@ The function should return a `string` containing the resource content (for examp
480592

481593
::: zone-end
482594

483-
::: zone pivot="programming-language-csharp,programming-language-python,programming-language-javascript,programming-language-typescript"
595+
::: zone pivot="programming-language-java"
596+
597+
The MCP resource trigger supports the following return types:
598+
599+
| Type | Description |
600+
| --- | --- |
601+
| `String` | Returned as text content in the MCP `ReadResourceResult`. |
602+
| `byte[]` | Returned as base64-encoded binary content in the MCP `ReadResourceResult`. Set `dataType = "binary"` on the annotation when returning binary content. |
603+
604+
::: zone-end
605+
606+
::: zone pivot="programming-language-csharp,programming-language-java,programming-language-python,programming-language-javascript,programming-language-typescript"
484607

485608
### Resource discovery
486609

0 commit comments

Comments
 (0)