Adds optional _meta field to Tool (#339)

<!-- Provide a brief summary of your changes -->

## Motivation and Context
The recently released openAI [Apps SDK
](https://developers.openai.com/apps-sdk/reference/#_meta-fields-the-client-provides)adds
a '__meta_' field on the Tool Descriptor. This would be in addition to
it being available on Messages. It remains optional and defaults to an
empty json object, following existing conventions.


## How Has This Been Tested?
Unit tests for serialization added

## Breaking Changes
N/A

## Types of changes
<!-- What types of changes does your code introduce? Put an `x` in all
the boxes that apply: -->
- [ ] Bug fix (non-breaking change which fixes an issue)
- [X] New feature (non-breaking change which adds functionality)
- [ ] Breaking change (fix or feature that would cause existing
functionality to change)
- [ ] Documentation update

## Checklist
<!-- Go over all the following points, and put an `x` in all the boxes
that apply. -->
- [X] I have read the [MCP
Documentation](https://modelcontextprotocol.io)
- [X] My code follows the repository's style guidelines
- [X] New and existing tests pass locally
- [X] I have added appropriate error handling
- [X] I have added or updated documentation as needed

## Additional context
https://developers.openai.com/apps-sdk

https://developers.openai.com/apps-sdk/reference/#_meta-fields-the-client-provides

---------

Co-authored-by: Pavel Gorgulov <devcrocod@gmail.com>
This commit is contained in:
Jeff Clyne 2025-11-04 11:39:22 -06:00 • committed by GitHub
commit e4bd866fdd
No known key found for this signature in database
GPG key ID: B5690EEEBB952194

View file

@ -3,6 +3,7 @@ package io.modelcontextprotocol.kotlin.sdk.server
import io.github.oshai.kotlinlogging.KotlinLogging
import io.modelcontextprotocol.kotlin.sdk.CallToolRequest
import io.modelcontextprotocol.kotlin.sdk.CallToolResult
import io.modelcontextprotocol.kotlin.sdk.EmptyJsonObject
import io.modelcontextprotocol.kotlin.sdk.GetPromptRequest
import io.modelcontextprotocol.kotlin.sdk.GetPromptResult
import io.modelcontextprotocol.kotlin.sdk.Implementation
@ -30,6 +31,7 @@ import kotlinx.atomicfu.atomic
import kotlinx.atomicfu.update
import kotlinx.collections.immutable.persistentListOf
import kotlinx.coroutines.CancellationException
import kotlinx.serialization.json.JsonObject
private val logger = KotlinLogging.logger {}
@ -243,6 +245,7 @@ public open class Server(
* @param inputSchema The expected input schema for the tool.
* @param outputSchema The optional expected output schema for the tool.
* @param toolAnnotations Optional additional tool information.
* @param _meta Optional metadata as a [JsonObject].
* @param handler A suspend function that handles executing the tool when called by the client.
* @throws IllegalStateException If the server does not support tools.
*/
@ -254,9 +257,18 @@ public open class Server(
title: String? = null,
outputSchema: Tool.Output? = null,
toolAnnotations: ToolAnnotations? = null,
@Suppress("LocalVariableName") _meta: JsonObject? = null,
handler: suspend (CallToolRequest) -> CallToolResult,
) {
val tool = Tool(name, title, description, inputSchema, outputSchema, toolAnnotations)
val tool = Tool(
name = name,
title = title,
description = description,
inputSchema = inputSchema,
outputSchema = outputSchema,
annotations = toolAnnotations,
_meta = _meta ?: EmptyJsonObject,
)
addTool(tool, handler)
}