Implement SEP-1577: Sampling With Tools (#628)

* feat: implement SEP-1577 sampling with tools support

* feat: add TryFrom<Content> for backward-compatible migration
This commit is contained in:
Dale Seo 2026-02-06 06:51:26 -05:00 • committed by GitHub
commit 8bd3fcb890
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
4 changed files with 1850 additions and 141 deletions

View file

@ -318,11 +318,15 @@
]
},
"sampling": {
"type": [
"object",
"null"
],
"additionalProperties": true
"description": "Capability for LLM sampling requests (SEP-1577)",
"anyOf": [
{
"$ref": "#/definitions/SamplingCapability"
},
{
"type": "null"
}
]
},
"tasks": {
"anyOf": [
@ -431,14 +435,21 @@
]
},
"CreateMessageResult": {
"description": "The result of a sampling/createMessage request containing the generated response.\n\nThis structure contains the generated message along with metadata about\nhow the generation was performed and why it stopped.",
"description": "Result of sampling/createMessage (SEP-1577).\nThe result of a sampling/createMessage request containing the generated response.\n\nThis structure contains the generated message along with metadata about\nhow the generation was performed and why it stopped.",
"type": "object",
"properties": {
"_meta": {
"type": [
"object",
"null"
],
"additionalProperties": true
},
"content": {
"description": "The actual content of the message (text, image, etc.)",
"description": "The actual content of the message (text, image, audio, tool use, or tool result)",
"allOf": [
{
"$ref": "#/definitions/Annotated"
"$ref": "#/definitions/SamplingContent"
}
]
},
@ -1741,6 +1752,134 @@
"format": "const",
"const": "notifications/roots/list_changed"
},
"SamplingCapability": {
"description": "Sampling capability with optional sub-capabilities (SEP-1577).",
"type": "object",
"properties": {
"context": {
"description": "Support for `includeContext` (soft-deprecated)",
"type": [
"object",
"null"
],
"additionalProperties": true
},
"tools": {
"description": "Support for `tools` and `toolChoice` parameters",
"type": [
"object",
"null"
],
"additionalProperties": true
}
}
},
"SamplingContent": {
"description": "Single or array content wrapper (SEP-1577).",
"anyOf": [
{
"$ref": "#/definitions/SamplingMessageContent"
},
{
"type": "array",
"items": {
"$ref": "#/definitions/SamplingMessageContent"
}
}
]
},
"SamplingMessageContent": {
"description": "Content types for sampling messages (SEP-1577).",
"oneOf": [
{
"type": "object",
"properties": {
"type": {
"type": "string",
"const": "text"
}
},
"allOf": [
{
"$ref": "#/definitions/RawTextContent"
}
],
"required": [
"type"
]
},
{
"type": "object",
"properties": {
"type": {
"type": "string",
"const": "image"
}
},
"allOf": [
{
"$ref": "#/definitions/RawImageContent"
}
],
"required": [
"type"
]
},
{
"type": "object",
"properties": {
"type": {
"type": "string",
"const": "audio"
}
},
"allOf": [
{
"$ref": "#/definitions/RawAudioContent"
}
],
"required": [
"type"
]
},
{
"description": "Assistant only",
"type": "object",
"properties": {
"type": {
"type": "string",
"const": "tool_use"
}
},
"allOf": [
{
"$ref": "#/definitions/ToolUseContent"
}
],
"required": [
"type"
]
},
{
"description": "User only",
"type": "object",
"properties": {
"type": {
"type": "string",
"const": "tool_result"
}
},
"allOf": [
{
"$ref": "#/definitions/ToolResultContent"
}
],
"required": [
"type"
]
}
]
},
"SamplingTaskCapability": {
"type": "object",
"properties": {
@ -1875,6 +2014,81 @@
}
}
},
"ToolResultContent": {
"description": "Tool execution result in user message (SEP-1577).",
"type": "object",
"properties": {
"_meta": {
"description": "Optional metadata",
"type": [
"object",
"null"
],
"additionalProperties": true
},
"content": {
"description": "Content blocks returned by the tool",
"type": "array",
"items": {
"$ref": "#/definitions/Annotated"
}
},
"isError": {
"description": "Whether tool execution failed",
"type": [
"boolean",
"null"
]
},
"structuredContent": {
"description": "Optional structured result",
"type": [
"object",
"null"
],
"additionalProperties": true
},
"toolUseId": {
"description": "ID of the corresponding tool use",
"type": "string"
}
},
"required": [
"toolUseId"
]
},
"ToolUseContent": {
"description": "Tool call request from assistant (SEP-1577).",
"type": "object",
"properties": {
"_meta": {
"description": "Optional metadata (preserved for caching)",
"type": [
"object",
"null"
],
"additionalProperties": true
},
"id": {
"description": "Unique identifier for this tool call",
"type": "string"
},
"input": {
"description": "Input arguments for the tool",
"type": "object",
"additionalProperties": true
},
"name": {
"description": "Name of the tool to call",
"type": "string"
}
},
"required": [
"id",
"name",
"input"
]
},
"ToolsTaskCapability": {
"type": "object",
"properties": {

View file

@ -318,11 +318,15 @@
]
},
"sampling": {
"type": [
"object",
"null"
],
"additionalProperties": true
"description": "Capability for LLM sampling requests (SEP-1577)",
"anyOf": [
{
"$ref": "#/definitions/SamplingCapability"
},
{
"type": "null"
}
]
},
"tasks": {
"anyOf": [
@ -431,14 +435,21 @@
]
},
"CreateMessageResult": {
"description": "The result of a sampling/createMessage request containing the generated response.\n\nThis structure contains the generated message along with metadata about\nhow the generation was performed and why it stopped.",
"description": "Result of sampling/createMessage (SEP-1577).\nThe result of a sampling/createMessage request containing the generated response.\n\nThis structure contains the generated message along with metadata about\nhow the generation was performed and why it stopped.",
"type": "object",
"properties": {
"_meta": {
"type": [
"object",
"null"
],
"additionalProperties": true
},
"content": {
"description": "The actual content of the message (text, image, etc.)",
"description": "The actual content of the message (text, image, audio, tool use, or tool result)",
"allOf": [
{
"$ref": "#/definitions/Annotated"
"$ref": "#/definitions/SamplingContent"
}
]
},
@ -1741,6 +1752,134 @@
"format": "const",
"const": "notifications/roots/list_changed"
},
"SamplingCapability": {
"description": "Sampling capability with optional sub-capabilities (SEP-1577).",
"type": "object",
"properties": {
"context": {
"description": "Support for `includeContext` (soft-deprecated)",
"type": [
"object",
"null"
],
"additionalProperties": true
},
"tools": {
"description": "Support for `tools` and `toolChoice` parameters",
"type": [
"object",
"null"
],
"additionalProperties": true
}
}
},
"SamplingContent": {
"description": "Single or array content wrapper (SEP-1577).",
"anyOf": [
{
"$ref": "#/definitions/SamplingMessageContent"
},
{
"type": "array",
"items": {
"$ref": "#/definitions/SamplingMessageContent"
}
}
]
},
"SamplingMessageContent": {
"description": "Content types for sampling messages (SEP-1577).",
"oneOf": [
{
"type": "object",
"properties": {
"type": {
"type": "string",
"const": "text"
}
},
"allOf": [
{
"$ref": "#/definitions/RawTextContent"
}
],
"required": [
"type"
]
},
{
"type": "object",
"properties": {
"type": {
"type": "string",
"const": "image"
}
},
"allOf": [
{
"$ref": "#/definitions/RawImageContent"
}
],
"required": [
"type"
]
},
{
"type": "object",
"properties": {
"type": {
"type": "string",
"const": "audio"
}
},
"allOf": [
{
"$ref": "#/definitions/RawAudioContent"
}
],
"required": [
"type"
]
},
{
"description": "Assistant only",
"type": "object",
"properties": {
"type": {
"type": "string",
"const": "tool_use"
}
},
"allOf": [
{
"$ref": "#/definitions/ToolUseContent"
}
],
"required": [
"type"
]
},
{
"description": "User only",
"type": "object",
"properties": {
"type": {
"type": "string",
"const": "tool_result"
}
},
"allOf": [
{
"$ref": "#/definitions/ToolResultContent"
}
],
"required": [
"type"
]
}
]
},
"SamplingTaskCapability": {
"type": "object",
"properties": {
@ -1875,6 +2014,81 @@
}
}
},
"ToolResultContent": {
"description": "Tool execution result in user message (SEP-1577).",
"type": "object",
"properties": {
"_meta": {
"description": "Optional metadata",
"type": [
"object",
"null"
],
"additionalProperties": true
},
"content": {
"description": "Content blocks returned by the tool",
"type": "array",
"items": {
"$ref": "#/definitions/Annotated"
}
},
"isError": {
"description": "Whether tool execution failed",
"type": [
"boolean",
"null"
]
},
"structuredContent": {
"description": "Optional structured result",
"type": [
"object",
"null"
],
"additionalProperties": true
},
"toolUseId": {
"description": "ID of the corresponding tool use",
"type": "string"
}
},
"required": [
"toolUseId"
]
},
"ToolUseContent": {
"description": "Tool call request from assistant (SEP-1577).",
"type": "object",
"properties": {
"_meta": {
"description": "Optional metadata (preserved for caching)",
"type": [
"object",
"null"
],
"additionalProperties": true
},
"id": {
"description": "Unique identifier for this tool call",
"type": "string"
},
"input": {
"description": "Input arguments for the tool",
"type": "object",
"additionalProperties": true
},
"name": {
"description": "Name of the tool to call",
"type": "string"
}
},
"required": [
"id",
"name",
"input"
]
},
"ToolsTaskCapability": {
"type": "object",
"properties": {

View file

@ -641,6 +641,27 @@
"null"
],
"format": "float"
},
"toolChoice": {
"description": "Tool selection behavior (SEP-1577)",
"anyOf": [
{
"$ref": "#/definitions/ToolChoice"
},
{
"type": "null"
}
]
},
"tools": {
"description": "Tools available for the model to call (SEP-1577)",
"type": [
"array",
"null"
],
"items": {
"$ref": "#/definitions/Tool"
}
}
},
"required": [
@ -2309,15 +2330,36 @@
}
]
},
"SamplingContent": {
"description": "Single or array content wrapper (SEP-1577).",
"anyOf": [
{
"$ref": "#/definitions/SamplingMessageContent"
},
{
"type": "array",
"items": {
"$ref": "#/definitions/SamplingMessageContent"
}
}
]
},
"SamplingMessage": {
"description": "A message in a sampling conversation, containing a role and content.\n\nThis represents a single message in a conversation flow, used primarily\nin LLM sampling requests where the conversation history is important\nfor generating appropriate responses.",
"type": "object",
"properties": {
"_meta": {
"type": [
"object",
"null"
],
"additionalProperties": true
},
"content": {
"description": "The actual content of the message (text, image, etc.)",
"description": "The actual content of the message (text, image, audio, tool use, or tool result)",
"allOf": [
{
"$ref": "#/definitions/Annotated"
"$ref": "#/definitions/SamplingContent"
}
]
},
@ -2335,6 +2377,98 @@
"content"
]
},
"SamplingMessageContent": {
"description": "Content types for sampling messages (SEP-1577).",
"oneOf": [
{
"type": "object",
"properties": {
"type": {
"type": "string",
"const": "text"
}
},
"allOf": [
{
"$ref": "#/definitions/RawTextContent"
}
],
"required": [
"type"
]
},
{
"type": "object",
"properties": {
"type": {
"type": "string",
"const": "image"
}
},
"allOf": [
{
"$ref": "#/definitions/RawImageContent"
}
],
"required": [
"type"
]
},
{
"type": "object",
"properties": {
"type": {
"type": "string",
"const": "audio"
}
},
"allOf": [
{
"$ref": "#/definitions/RawAudioContent"
}
],
"required": [
"type"
]
},
{
"description": "Assistant only",
"type": "object",
"properties": {
"type": {
"type": "string",
"const": "tool_use"
}
},
"allOf": [
{
"$ref": "#/definitions/ToolUseContent"
}
],
"required": [
"type"
]
},
{
"description": "User only",
"type": "object",
"properties": {
"type": {
"type": "string",
"const": "tool_result"
}
},
"allOf": [
{
"$ref": "#/definitions/ToolResultContent"
}
],
"required": [
"type"
]
}
]
},
"SamplingTaskCapability": {
"type": "object",
"properties": {
@ -3008,6 +3142,42 @@
}
}
},
"ToolChoice": {
"description": "Tool choice configuration (SEP-1577).",
"type": "object",
"properties": {
"mode": {
"anyOf": [
{
"$ref": "#/definitions/ToolChoiceMode"
},
{
"type": "null"
}
]
}
}
},
"ToolChoiceMode": {
"description": "Tool selection mode (SEP-1577).",
"oneOf": [
{
"description": "Model decides whether to use tools",
"type": "string",
"const": "auto"
},
{
"description": "Model must use at least one tool",
"type": "string",
"const": "required"
},
{
"description": "Model must not use tools",
"type": "string",
"const": "none"
}
]
},
"ToolExecution": {
"description": "Execution-related configuration for a tool.\n\nThis struct contains settings that control how a tool should be executed,\nincluding task support configuration.",
"type": "object",
@ -3030,6 +3200,81 @@
"format": "const",
"const": "notifications/tools/list_changed"
},
"ToolResultContent": {
"description": "Tool execution result in user message (SEP-1577).",
"type": "object",
"properties": {
"_meta": {
"description": "Optional metadata",
"type": [
"object",
"null"
],
"additionalProperties": true
},
"content": {
"description": "Content blocks returned by the tool",
"type": "array",
"items": {
"$ref": "#/definitions/Annotated"
}
},
"isError": {
"description": "Whether tool execution failed",
"type": [
"boolean",
"null"
]
},
"structuredContent": {
"description": "Optional structured result",
"type": [
"object",
"null"
],
"additionalProperties": true
},
"toolUseId": {
"description": "ID of the corresponding tool use",
"type": "string"
}
},
"required": [
"toolUseId"
]
},
"ToolUseContent": {
"description": "Tool call request from assistant (SEP-1577).",
"type": "object",
"properties": {
"_meta": {
"description": "Optional metadata (preserved for caching)",
"type": [
"object",
"null"
],
"additionalProperties": true
},
"id": {
"description": "Unique identifier for this tool call",
"type": "string"
},
"input": {
"description": "Input arguments for the tool",
"type": "object",
"additionalProperties": true
},
"name": {
"description": "Name of the tool to call",
"type": "string"
}
},
"required": [
"id",
"name",
"input"
]
},
"ToolsCapability": {
"type": "object",
"properties": {

View file

@ -641,6 +641,27 @@
"null"
],
"format": "float"
},
"toolChoice": {
"description": "Tool selection behavior (SEP-1577)",
"anyOf": [
{
"$ref": "#/definitions/ToolChoice"
},
{
"type": "null"
}
]
},
"tools": {
"description": "Tools available for the model to call (SEP-1577)",
"type": [
"array",
"null"
],
"items": {
"$ref": "#/definitions/Tool"
}
}
},
"required": [
@ -2309,15 +2330,36 @@
}
]
},
"SamplingContent": {
"description": "Single or array content wrapper (SEP-1577).",
"anyOf": [
{
"$ref": "#/definitions/SamplingMessageContent"
},
{
"type": "array",
"items": {
"$ref": "#/definitions/SamplingMessageContent"
}
}
]
},
"SamplingMessage": {
"description": "A message in a sampling conversation, containing a role and content.\n\nThis represents a single message in a conversation flow, used primarily\nin LLM sampling requests where the conversation history is important\nfor generating appropriate responses.",
"type": "object",
"properties": {
"_meta": {
"type": [
"object",
"null"
],
"additionalProperties": true
},
"content": {
"description": "The actual content of the message (text, image, etc.)",
"description": "The actual content of the message (text, image, audio, tool use, or tool result)",
"allOf": [
{
"$ref": "#/definitions/Annotated"
"$ref": "#/definitions/SamplingContent"
}
]
},
@ -2335,6 +2377,98 @@
"content"
]
},
"SamplingMessageContent": {
"description": "Content types for sampling messages (SEP-1577).",
"oneOf": [
{
"type": "object",
"properties": {
"type": {
"type": "string",
"const": "text"
}
},
"allOf": [
{
"$ref": "#/definitions/RawTextContent"
}
],
"required": [
"type"
]
},
{
"type": "object",
"properties": {
"type": {
"type": "string",
"const": "image"
}
},
"allOf": [
{
"$ref": "#/definitions/RawImageContent"
}
],
"required": [
"type"
]
},
{
"type": "object",
"properties": {
"type": {
"type": "string",
"const": "audio"
}
},
"allOf": [
{
"$ref": "#/definitions/RawAudioContent"
}
],
"required": [
"type"
]
},
{
"description": "Assistant only",
"type": "object",
"properties": {
"type": {
"type": "string",
"const": "tool_use"
}
},
"allOf": [
{
"$ref": "#/definitions/ToolUseContent"
}
],
"required": [
"type"
]
},
{
"description": "User only",
"type": "object",
"properties": {
"type": {
"type": "string",
"const": "tool_result"
}
},
"allOf": [
{
"$ref": "#/definitions/ToolResultContent"
}
],
"required": [
"type"
]
}
]
},
"SamplingTaskCapability": {
"type": "object",
"properties": {
@ -3008,6 +3142,42 @@
}
}
},
"ToolChoice": {
"description": "Tool choice configuration (SEP-1577).",
"type": "object",
"properties": {
"mode": {
"anyOf": [
{
"$ref": "#/definitions/ToolChoiceMode"
},
{
"type": "null"
}
]
}
}
},
"ToolChoiceMode": {
"description": "Tool selection mode (SEP-1577).",
"oneOf": [
{
"description": "Model decides whether to use tools",
"type": "string",
"const": "auto"
},
{
"description": "Model must use at least one tool",
"type": "string",
"const": "required"
},
{
"description": "Model must not use tools",
"type": "string",
"const": "none"
}
]
},
"ToolExecution": {
"description": "Execution-related configuration for a tool.\n\nThis struct contains settings that control how a tool should be executed,\nincluding task support configuration.",
"type": "object",
@ -3030,6 +3200,81 @@
"format": "const",
"const": "notifications/tools/list_changed"
},
"ToolResultContent": {
"description": "Tool execution result in user message (SEP-1577).",
"type": "object",
"properties": {
"_meta": {
"description": "Optional metadata",
"type": [
"object",
"null"
],
"additionalProperties": true
},
"content": {
"description": "Content blocks returned by the tool",
"type": "array",
"items": {
"$ref": "#/definitions/Annotated"
}
},
"isError": {
"description": "Whether tool execution failed",
"type": [
"boolean",
"null"
]
},
"structuredContent": {
"description": "Optional structured result",
"type": [
"object",
"null"
],
"additionalProperties": true
},
"toolUseId": {
"description": "ID of the corresponding tool use",
"type": "string"
}
},
"required": [
"toolUseId"
]
},
"ToolUseContent": {
"description": "Tool call request from assistant (SEP-1577).",
"type": "object",
"properties": {
"_meta": {
"description": "Optional metadata (preserved for caching)",
"type": [
"object",
"null"
],
"additionalProperties": true
},
"id": {
"description": "Unique identifier for this tool call",
"type": "string"
},
"input": {
"description": "Input arguments for the tool",
"type": "object",
"additionalProperties": true
},
"name": {
"description": "Name of the tool to call",
"type": "string"
}
},
"required": [
"id",
"name",
"input"
]
},
"ToolsCapability": {
"type": "object",
"properties": {