Azure / Azure/azure-functions-java-worker

[MCP Tool] Implement ability to specify outputSchema on tools

Đang mở
#858 0 bình luận 0 reaction 1 người được giao Được @ahmedmuhsin nhận Xem trên GitHub
mcp-extension
Ngôn ngữ chính
Java
Star
103
Fork
74
Merge trung bình
4 ngày 8 giờ
Pull request đã merge (30 ngày)
2

Mô tả

Implement support for specifying an `outputSchema` on MCP tool definitions. This allows tools to declare the JSON schema of their structured output, enabling clients to validate and better understand tool responses.

#### Background

The MCP specification allows tools to declare an output schema that describes the structure of the tool's return value. This is separate from the input schema (which describes the tool's parameters).

**MCP Specification:** https://modelcontextprotocol.io/specification/2025-06-18/server/tools#output-schema

## Output Schema: Worker ↔ Host Contract (based off of [this PR](https://github.com/Azure/azure-functions-mcp-extension/pull/246))

### 1. Worker side — configuration

The user calls the fluent builder:

```csharp
builder.ConfigureMcpTool("MyTool")
.WithOutputSchema("""{ "type": "object", "properties": { ... } }""");
```

`WithOutputSchema` validates the JSON string and stores it as `McpOutputSchema` on `ToolOptions`.

### 2. Worker side — metadata emission

During function metadata transformation, `ResolveToolOutputSchemaExtension` reads `ToolOptions.OutputSchema` and writes its validated JSON string onto `McpParsedBinding.OutputSchema`. `McpBindingBuilder.Build()` then injects it into the raw binding JSON under the key:

```json
"outputSchema": ""
```

No flag (analogous to `useWorkerInputSchema`) is needed — if the key is present there is a schema; if absent, there isn't.

### 3. Host side — reading the contract

`McpToolTriggerAttribute` exposes `OutputSchema` (`string?`) populated from the raw binding JSON. `McpToolTriggerBinding.GetOutputSchema()` parses and validates it, requiring:
- Valid JSON
- Root must be `type: "object"` (validated via `McpInputSchemaJsonUtilities.IsValidMcpToolJsonSchema`)

The resulting `JsonElement?` is passed to `McpToolListener`, which advertises it as the tool's `outputSchema` in the MCP `tools/list` response.

### Summary

| Step | Location | Binding key |
|---|---|---|
| User configures schema | `McpToolBuilder.WithOutputSchema()` (worker) | — |
| Schema validated + stored | `McpOutputSchema` / `McpToolSchemaValidator` (worker) | — |
| Schema written to raw binding | `ResolveToolOutputSchemaExtension` → `McpBindingBuilder` (worker) | `outputSchema` |
| Host reads from binding attr | `McpToolTriggerAttribute.OutputSchema` (host) | `outputSchema` |
| Host validates + registers | `McpToolTriggerBinding.GetOutputSchema()` (host) | — |
| Host advertises to MCP client | `McpToolListener` → `tools/list` response (host) | `outputSchema` in tool descriptor |

Hướng dẫn đóng góp

Mở hướng dẫn đóng góp

Đánh giá

Issue này chưa được đánh giá.

Nhận issue mới trong hộp thư của bạn

Bản tóm tắt ngắn những issue GitHub phù hợp với người mới.