Implementation of resources. This follows the design sketched in design/design.md, except for Server.FileResourceHandler, which will be in a forthcoming CL. As the spec recommends, the server verifies an incoming URI from the client against its list of resources, to make sure the URI came from one of them. Some things that aren't in the design doc: - I provide an exported function that creates a "resource not found" jsonrpc2.WireEror error for server authors to return. A wrinkle: the MCP spec defines the error code for "resource not found" as -32002 [1]. That code is reserved for use by implementations of JSON-RPC, not applications. That matters to us because our JSON-RPC implementation already uses that code for the "server closing" error. I filed a bug against MCP about it [2] and chose a different code for the "resource not found" error. - As a slight convenience, I fill in some fields in the ReadResourceResult if the handler doesn't set them. [1] https://modelcontextprotocol.io/specification/2025-03-26/server/resources#error-handling [2] https://github.com/modelcontextprotocol/modelcontextprotocol/issues/509 Change-Id: I06153da982ea00748a3691ded923448a880cea2a Reviewed-on: https://go-review.googlesource.com/c/tools/+/671363 LUCI-TryBot-Result: Go LUCI <golang-scoped@luci-project-accounts.iam.gserviceaccount.com> Reviewed-by: Robert Findley <rfindley@google.com>
387 lines
15 KiB
Go
387 lines
15 KiB
Go
// Copyright 2025 The Go MCP SDK Authors. All rights reserved.
|
|
// Use of this source code is governed by an MIT-style
|
|
// license that can be found in the LICENSE file.
|
|
|
|
// Code generated by generate.go. DO NOT EDIT.
|
|
|
|
package protocol
|
|
|
|
import (
|
|
"encoding/json"
|
|
|
|
"github.com/modelcontextprotocol/go-sdk/jsonschema"
|
|
)
|
|
|
|
// Optional annotations for the client. The client can use annotations to inform
|
|
// how objects are used or displayed
|
|
type Annotations struct {
|
|
// Describes who the intended customer of this object or data is.
|
|
//
|
|
// It can include multiple entries to indicate content useful for multiple
|
|
// audiences (e.g., `["user", "assistant"]`).
|
|
Audience []Role `json:"audience,omitempty"`
|
|
// Describes how important this data is for operating the server.
|
|
//
|
|
// A value of 1 means "most important," and indicates that the data is
|
|
// effectively required, while 0 means "least important," and indicates that the
|
|
// data is entirely optional.
|
|
Priority float64 `json:"priority,omitempty"`
|
|
}
|
|
|
|
type CallToolParams struct {
|
|
Arguments map[string]json.RawMessage `json:"arguments,omitempty"`
|
|
Name string `json:"name"`
|
|
}
|
|
|
|
// The server's response to a tool call.
|
|
//
|
|
// Any errors that originate from the tool SHOULD be reported inside the result
|
|
// object, with `isError` set to true, _not_ as an MCP protocol-level error
|
|
// response. Otherwise, the LLM would not be able to see that an error occurred
|
|
// and self-correct.
|
|
//
|
|
// However, any errors in _finding_ the tool, an error indicating that the
|
|
// server does not support tool calls, or any other exceptional conditions,
|
|
// should be reported as an MCP error response.
|
|
type CallToolResult struct {
|
|
// This result property is reserved by the protocol to allow clients and servers
|
|
// to attach additional metadata to their responses.
|
|
Meta map[string]json.RawMessage `json:"_meta,omitempty"`
|
|
Content []Content `json:"content"`
|
|
// Whether the tool call ended in an error.
|
|
//
|
|
// If not set, this is assumed to be false (the call was successful).
|
|
IsError bool `json:"isError,omitempty"`
|
|
}
|
|
|
|
type CancelledParams struct {
|
|
// An optional string describing the reason for the cancellation. This MAY be
|
|
// logged or presented to the user.
|
|
Reason string `json:"reason,omitempty"`
|
|
// The ID of the request to cancel.
|
|
//
|
|
// This MUST correspond to the ID of a request previously issued in the same
|
|
// direction.
|
|
RequestID any `json:"requestId"`
|
|
}
|
|
|
|
// Capabilities a client may support. Known capabilities are defined here, in
|
|
// this schema, but this is not a closed set: any client can define its own,
|
|
// additional capabilities.
|
|
type ClientCapabilities struct {
|
|
// Experimental, non-standard capabilities that the client supports.
|
|
Experimental map[string]struct {
|
|
} `json:"experimental,omitempty"`
|
|
// Present if the client supports listing roots.
|
|
Roots *struct {
|
|
// Whether the client supports notifications for changes to the roots list.
|
|
ListChanged bool `json:"listChanged,omitempty"`
|
|
} `json:"roots,omitempty"`
|
|
// Present if the client supports sampling from an LLM.
|
|
Sampling struct {
|
|
} `json:"sampling,omitempty"`
|
|
}
|
|
|
|
type GetPromptParams struct {
|
|
// Arguments to use for templating the prompt.
|
|
Arguments map[string]string `json:"arguments,omitempty"`
|
|
// The name of the prompt or prompt template.
|
|
Name string `json:"name"`
|
|
}
|
|
|
|
// The server's response to a prompts/get request from the client.
|
|
type GetPromptResult struct {
|
|
// This result property is reserved by the protocol to allow clients and servers
|
|
// to attach additional metadata to their responses.
|
|
Meta map[string]json.RawMessage `json:"_meta,omitempty"`
|
|
// An optional description for the prompt.
|
|
Description string `json:"description,omitempty"`
|
|
Messages []PromptMessage `json:"messages"`
|
|
}
|
|
|
|
// Describes the name and version of an MCP implementation.
|
|
type Implementation struct {
|
|
Name string `json:"name"`
|
|
Version string `json:"version"`
|
|
}
|
|
|
|
type InitializeParams struct {
|
|
Capabilities ClientCapabilities `json:"capabilities"`
|
|
ClientInfo Implementation `json:"clientInfo"`
|
|
// The latest version of the Model Context Protocol that the client supports.
|
|
// The client MAY decide to support older versions as well.
|
|
ProtocolVersion string `json:"protocolVersion"`
|
|
}
|
|
|
|
// After receiving an initialize request from the client, the server sends this
|
|
// response.
|
|
type InitializeResult struct {
|
|
// This result property is reserved by the protocol to allow clients and servers
|
|
// to attach additional metadata to their responses.
|
|
Meta map[string]json.RawMessage `json:"_meta,omitempty"`
|
|
Capabilities ServerCapabilities `json:"capabilities"`
|
|
// Instructions describing how to use the server and its features.
|
|
//
|
|
// This can be used by clients to improve the LLM's understanding of available
|
|
// tools, resources, etc. It can be thought of like a "hint" to the model. For
|
|
// example, this information MAY be added to the system prompt.
|
|
Instructions string `json:"instructions,omitempty"`
|
|
// The version of the Model Context Protocol that the server wants to use. This
|
|
// may not match the version that the client requested. If the client cannot
|
|
// support this version, it MUST disconnect.
|
|
ProtocolVersion string `json:"protocolVersion"`
|
|
ServerInfo Implementation `json:"serverInfo"`
|
|
}
|
|
|
|
type InitializedParams struct {
|
|
// This parameter name is reserved by MCP to allow clients and servers to attach
|
|
// additional metadata to their notifications.
|
|
Meta map[string]json.RawMessage `json:"_meta,omitempty"`
|
|
}
|
|
|
|
type ListPromptsParams struct {
|
|
// An opaque token representing the current pagination position. If provided,
|
|
// the server should return results starting after this cursor.
|
|
Cursor string `json:"cursor,omitempty"`
|
|
}
|
|
|
|
// The server's response to a prompts/list request from the client.
|
|
type ListPromptsResult struct {
|
|
// This result property is reserved by the protocol to allow clients and servers
|
|
// to attach additional metadata to their responses.
|
|
Meta map[string]json.RawMessage `json:"_meta,omitempty"`
|
|
// An opaque token representing the pagination position after the last returned
|
|
// result. If present, there may be more results available.
|
|
NextCursor string `json:"nextCursor,omitempty"`
|
|
Prompts []Prompt `json:"prompts"`
|
|
}
|
|
|
|
type ListResourcesParams struct {
|
|
// An opaque token representing the current pagination position. If provided,
|
|
// the server should return results starting after this cursor.
|
|
Cursor string `json:"cursor,omitempty"`
|
|
}
|
|
|
|
// The server's response to a resources/list request from the client.
|
|
type ListResourcesResult struct {
|
|
// This result property is reserved by the protocol to allow clients and servers
|
|
// to attach additional metadata to their responses.
|
|
Meta map[string]json.RawMessage `json:"_meta,omitempty"`
|
|
// An opaque token representing the pagination position after the last returned
|
|
// result. If present, there may be more results available.
|
|
NextCursor string `json:"nextCursor,omitempty"`
|
|
Resources []Resource `json:"resources"`
|
|
}
|
|
|
|
type ListRootsParams struct {
|
|
Meta *struct {
|
|
// If specified, the caller is requesting out-of-band progress notifications for
|
|
// this request (as represented by notifications/progress). The value of this
|
|
// parameter is an opaque token that will be attached to any subsequent
|
|
// notifications. The receiver is not obligated to provide these notifications.
|
|
ProgressToken *any `json:"progressToken,omitempty"`
|
|
} `json:"_meta,omitempty"`
|
|
}
|
|
|
|
// The client's response to a roots/list request from the server. This result
|
|
// contains an array of Root objects, each representing a root directory or file
|
|
// that the server can operate on.
|
|
type ListRootsResult struct {
|
|
// This result property is reserved by the protocol to allow clients and servers
|
|
// to attach additional metadata to their responses.
|
|
Meta map[string]json.RawMessage `json:"_meta,omitempty"`
|
|
Roots []Root `json:"roots"`
|
|
}
|
|
|
|
type ListToolsParams struct {
|
|
// An opaque token representing the current pagination position. If provided,
|
|
// the server should return results starting after this cursor.
|
|
Cursor string `json:"cursor,omitempty"`
|
|
}
|
|
|
|
// The server's response to a tools/list request from the client.
|
|
type ListToolsResult struct {
|
|
// This result property is reserved by the protocol to allow clients and servers
|
|
// to attach additional metadata to their responses.
|
|
Meta map[string]json.RawMessage `json:"_meta,omitempty"`
|
|
// An opaque token representing the pagination position after the last returned
|
|
// result. If present, there may be more results available.
|
|
NextCursor string `json:"nextCursor,omitempty"`
|
|
Tools []Tool `json:"tools"`
|
|
}
|
|
|
|
// A prompt or prompt template that the server offers.
|
|
type Prompt struct {
|
|
// A list of arguments to use for templating the prompt.
|
|
Arguments []PromptArgument `json:"arguments,omitempty"`
|
|
// An optional description of what this prompt provides
|
|
Description string `json:"description,omitempty"`
|
|
// The name of the prompt or prompt template.
|
|
Name string `json:"name"`
|
|
}
|
|
|
|
// Describes an argument that a prompt can accept.
|
|
type PromptArgument struct {
|
|
// A human-readable description of the argument.
|
|
Description string `json:"description,omitempty"`
|
|
// The name of the argument.
|
|
Name string `json:"name"`
|
|
// Whether this argument must be provided.
|
|
Required bool `json:"required,omitempty"`
|
|
}
|
|
|
|
// Present if the server offers any prompt templates.
|
|
type PromptCapabilities struct {
|
|
// Whether this server supports notifications for changes to the prompt list.
|
|
ListChanged bool `json:"listChanged,omitempty"`
|
|
}
|
|
|
|
// Describes a message returned as part of a prompt.
|
|
//
|
|
// This is similar to `SamplingMessage`, but also supports the embedding of
|
|
// resources from the MCP server.
|
|
type PromptMessage struct {
|
|
Content Content `json:"content"`
|
|
Role Role `json:"role"`
|
|
}
|
|
|
|
type ReadResourceParams struct {
|
|
// The URI of the resource to read. The URI can use any protocol; it is up to
|
|
// the server how to interpret it.
|
|
URI string `json:"uri"`
|
|
}
|
|
|
|
// The server's response to a resources/read request from the client.
|
|
type ReadResourceResult struct {
|
|
// This result property is reserved by the protocol to allow clients and servers
|
|
// to attach additional metadata to their responses.
|
|
Meta map[string]json.RawMessage `json:"_meta,omitempty"`
|
|
Contents *ResourceContents `json:"contents"`
|
|
}
|
|
|
|
// A known resource that the server is capable of reading.
|
|
type Resource struct {
|
|
// Optional annotations for the client.
|
|
Annotations *Annotations `json:"annotations,omitempty"`
|
|
// A description of what this resource represents.
|
|
//
|
|
// This can be used by clients to improve the LLM's understanding of available
|
|
// resources. It can be thought of like a "hint" to the model.
|
|
Description string `json:"description,omitempty"`
|
|
// The MIME type of this resource, if known.
|
|
MIMEType string `json:"mimeType,omitempty"`
|
|
// A human-readable name for this resource.
|
|
//
|
|
// This can be used by clients to populate UI elements.
|
|
Name string `json:"name"`
|
|
// The size of the raw resource content, in bytes (i.e., before base64 encoding
|
|
// or any tokenization), if known.
|
|
//
|
|
// This can be used by Hosts to display file sizes and estimate context window
|
|
// usage.
|
|
Size int64 `json:"size,omitempty"`
|
|
// The URI of this resource.
|
|
URI string `json:"uri"`
|
|
}
|
|
|
|
// Present if the server offers any resources to read.
|
|
type ResourceCapabilities struct {
|
|
// Whether this server supports notifications for changes to the resource list.
|
|
ListChanged bool `json:"listChanged,omitempty"`
|
|
// Whether this server supports subscribing to resource updates.
|
|
Subscribe bool `json:"subscribe,omitempty"`
|
|
}
|
|
|
|
// The sender or recipient of messages and data in a conversation.
|
|
type Role string
|
|
|
|
// Represents a root directory or file that the server can operate on.
|
|
type Root struct {
|
|
// An optional name for the root. This can be used to provide a human-readable
|
|
// identifier for the root, which may be useful for display purposes or for
|
|
// referencing the root in other parts of the application.
|
|
Name string `json:"name,omitempty"`
|
|
// The URI identifying the root. This *must* start with file:// for now. This
|
|
// restriction may be relaxed in future versions of the protocol to allow other
|
|
// URI schemes.
|
|
URI string `json:"uri"`
|
|
}
|
|
|
|
// Capabilities that a server may support. Known capabilities are defined here,
|
|
// in this schema, but this is not a closed set: any server can define its own,
|
|
// additional capabilities.
|
|
type ServerCapabilities struct {
|
|
// Present if the server supports argument autocompletion suggestions.
|
|
Completions struct {
|
|
} `json:"completions,omitempty"`
|
|
// Experimental, non-standard capabilities that the server supports.
|
|
Experimental map[string]struct {
|
|
} `json:"experimental,omitempty"`
|
|
// Present if the server supports sending log messages to the client.
|
|
Logging struct {
|
|
} `json:"logging,omitempty"`
|
|
// Present if the server offers any prompt templates.
|
|
Prompts *PromptCapabilities `json:"prompts,omitempty"`
|
|
// Present if the server offers any resources to read.
|
|
Resources *ResourceCapabilities `json:"resources,omitempty"`
|
|
// Present if the server offers any tools to call.
|
|
Tools *ToolCapabilities `json:"tools,omitempty"`
|
|
}
|
|
|
|
// Definition for a tool the client can call.
|
|
type Tool struct {
|
|
// Optional additional tool information.
|
|
Annotations *ToolAnnotations `json:"annotations,omitempty"`
|
|
// A human-readable description of the tool.
|
|
//
|
|
// This can be used by clients to improve the LLM's understanding of available
|
|
// tools. It can be thought of like a "hint" to the model.
|
|
Description string `json:"description,omitempty"`
|
|
// A JSON Schema object defining the expected parameters for the tool.
|
|
InputSchema *jsonschema.Schema `json:"inputSchema"`
|
|
// The name of the tool.
|
|
Name string `json:"name"`
|
|
}
|
|
|
|
// Additional properties describing a Tool to clients.
|
|
//
|
|
// NOTE: all properties in ToolAnnotations are **hints**. They are not
|
|
// guaranteed to provide a faithful description of tool behavior (including
|
|
// descriptive properties like `title`).
|
|
//
|
|
// Clients should never make tool use decisions based on ToolAnnotations
|
|
// received from untrusted servers.
|
|
type ToolAnnotations struct {
|
|
// If true, the tool may perform destructive updates to its environment. If
|
|
// false, the tool performs only additive updates.
|
|
//
|
|
// (This property is meaningful only when `readOnlyHint == false`)
|
|
//
|
|
// Default: true
|
|
DestructiveHint bool `json:"destructiveHint,omitempty"`
|
|
// If true, calling the tool repeatedly with the same arguments will have no
|
|
// additional effect on the its environment.
|
|
//
|
|
// (This property is meaningful only when `readOnlyHint == false`)
|
|
//
|
|
// Default: false
|
|
IdempotentHint bool `json:"idempotentHint,omitempty"`
|
|
// If true, this tool may interact with an "open world" of external entities. If
|
|
// false, the tool's domain of interaction is closed. For example, the world of
|
|
// a web search tool is open, whereas that of a memory tool is not.
|
|
//
|
|
// Default: true
|
|
OpenWorldHint bool `json:"openWorldHint,omitempty"`
|
|
// If true, the tool does not modify its environment.
|
|
//
|
|
// Default: false
|
|
ReadOnlyHint bool `json:"readOnlyHint,omitempty"`
|
|
// A human-readable title for the tool.
|
|
Title string `json:"title,omitempty"`
|
|
}
|
|
|
|
// Present if the server offers any tools to call.
|
|
type ToolCapabilities struct {
|
|
// Whether this server supports notifications for changes to the tool list.
|
|
ListChanged bool `json:"listChanged,omitempty"`
|
|
}
|