protocol

package
v1.0.2 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Jan 15, 2026 License: Apache-2.0 Imports: 3 Imported by: 0

Documentation

Overview

Copyright 2026 Teradata

Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance with the License. You may obtain a copy of the License at

http://www.apache.org/licenses/LICENSE-2.0

Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the specific language governing permissions and limitations under the License. Package protocol implements the Model Context Protocol (MCP) JSON-RPC 2.0 layer. This package provides types and utilities for MCP protocol communication.

Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance with the License. You may obtain a copy of the License at

http://www.apache.org/licenses/LICENSE-2.0

Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the specific language governing permissions and limitations under the License. Package protocol implements MCP protocol types for the Model Context Protocol.

Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance with the License. You may obtain a copy of the License at

http://www.apache.org/licenses/LICENSE-2.0

Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the specific language governing permissions and limitations under the License. Package protocol provides validation utilities for MCP protocol.

Index

Constants

View Source
const (
	ParseError     = -32700 // Invalid JSON
	InvalidRequest = -32600 // Invalid JSON-RPC
	MethodNotFound = -32601 // Method doesn't exist
	InvalidParams  = -32602 // Invalid parameters
	InternalError  = -32603 // Internal error
	ServerError    = -32000 // Server-specific error (to -32099)
)

Standard JSON-RPC error codes

View Source
const JSONRPCVersion = "2.0"

JSONRPCVersion is the required version string for JSON-RPC 2.0

View Source
const ProtocolVersion = "2024-11-05"

ProtocolVersion is the MCP protocol version supported by this implementation

Variables

This section is empty.

Functions

func ValidateRequest

func ValidateRequest(req *Request) error

ValidateRequest validates a JSON-RPC request

func ValidateResponse

func ValidateResponse(resp *Response) error

ValidateResponse validates a JSON-RPC response

func ValidateToolArguments

func ValidateToolArguments(tool Tool, arguments map[string]interface{}) error

ValidateToolArguments validates tool arguments against JSON Schema

Types

type CallToolParams

type CallToolParams struct {
	Name      string                 `json:"name"`
	Arguments map[string]interface{} `json:"arguments,omitempty"`
}

CallToolParams contains parameters for tools/call

type CallToolResult

type CallToolResult struct {
	Content []Content `json:"content"`           // Array of content items
	IsError bool      `json:"isError,omitempty"` // Deprecated, use proper errors
}

CallToolResult is the response from tools/call

type ClientCapabilities

type ClientCapabilities struct {
	Roots    *RootsCapability    `json:"roots,omitempty"`
	Sampling *SamplingCapability `json:"sampling,omitempty"`
}

ClientCapabilities declares what the client supports

type Content

type Content struct {
	Type     string       `json:"type"` // "text", "image", "resource"
	Text     string       `json:"text,omitempty"`
	Data     string       `json:"data,omitempty"`     // Base64 for images
	MimeType string       `json:"mimeType,omitempty"` // For images/resources
	Resource *ResourceRef `json:"resource,omitempty"` // For resource type
}

Content represents different types of content (text, image, resource)

type Error

type Error struct {
	Code    int             `json:"code"`           // Error code
	Message string          `json:"message"`        // Human-readable message
	Data    json.RawMessage `json:"data,omitempty"` // Additional error info
}

Error represents a JSON-RPC 2.0 error

func NewError

func NewError(code int, message string, data interface{}) *Error

NewError creates a standard JSON-RPC error

func (*Error) Error

func (e *Error) Error() string

Implement error interface for Error

type GetPromptParams

type GetPromptParams struct {
	Name      string                 `json:"name"`
	Arguments map[string]interface{} `json:"arguments,omitempty"`
}

GetPromptParams contains parameters for prompts/get

type GetPromptResult

type GetPromptResult struct {
	Description string          `json:"description,omitempty"`
	Messages    []PromptMessage `json:"messages"`
}

GetPromptResult is the response from prompts/get

type Implementation

type Implementation struct {
	Name    string `json:"name"`
	Version string `json:"version"`
}

Implementation describes client or server implementation details

type InitializeParams

type InitializeParams struct {
	ProtocolVersion string             `json:"protocolVersion"`
	Capabilities    ClientCapabilities `json:"capabilities"`
	ClientInfo      Implementation     `json:"clientInfo"`
}

InitializeParams contains parameters for the initialize request

type InitializeResult

type InitializeResult struct {
	ProtocolVersion string             `json:"protocolVersion"`
	Capabilities    ServerCapabilities `json:"capabilities"`
	ServerInfo      Implementation     `json:"serverInfo"`
}

InitializeResult contains the server's response to initialize

type LogNotification

type LogNotification struct {
	Level  string      `json:"level"` // "debug", "info", "warning", "error"
	Logger string      `json:"logger,omitempty"`
	Data   interface{} `json:"data"`
}

LogNotification sends log messages from server to client

type LoggingCapability

type LoggingCapability struct{}

type ModelHint

type ModelHint struct {
	Name string `json:"name,omitempty"`
}

ModelHint suggests model preferences

type ModelPreferences

type ModelPreferences struct {
	Hints                []ModelHint `json:"hints,omitempty"`
	CostPriority         *float64    `json:"costPriority,omitempty"`         // 0-1
	SpeedPriority        *float64    `json:"speedPriority,omitempty"`        // 0-1
	IntelligencePriority *float64    `json:"intelligencePriority,omitempty"` // 0-1
}

ModelPreferences specifies LLM selection preferences

type ProgressNotification

type ProgressNotification struct {
	ProgressToken string  `json:"progressToken"`
	Progress      float64 `json:"progress"`
	Total         float64 `json:"total,omitempty"`
}

ProgressNotification reports progress for a long-running operation

type Prompt

type Prompt struct {
	Name        string           `json:"name"`
	Description string           `json:"description,omitempty"`
	Arguments   []PromptArgument `json:"arguments,omitempty"`
}

Prompt represents an MCP prompt definition

type PromptArgument

type PromptArgument struct {
	Name        string `json:"name"`
	Description string `json:"description,omitempty"`
	Required    bool   `json:"required,omitempty"`
}

PromptArgument describes a prompt parameter

type PromptListChangedNotification

type PromptListChangedNotification struct{}

PromptListChangedNotification notifies that the prompt list has changed

type PromptListResult

type PromptListResult struct {
	Prompts []Prompt `json:"prompts"`
}

PromptListResult is the response from prompts/list

type PromptMessage

type PromptMessage struct {
	Role    string      `json:"role"`    // "user" or "assistant"
	Content interface{} `json:"content"` // Can be string or Content object
}

PromptMessage represents a message in a prompt

type PromptsCapability

type PromptsCapability struct {
	ListChanged bool `json:"listChanged,omitempty"` // Sends list change notifications
}

type ReadResourceParams

type ReadResourceParams struct {
	URI string `json:"uri"`
}

ReadResourceParams contains parameters for resources/read

type ReadResourceResult

type ReadResourceResult struct {
	Contents []ResourceContents `json:"contents"`
}

ReadResourceResult is the response from resources/read

type Request

type Request struct {
	JSONRPC string          `json:"jsonrpc"`          // Must be "2.0"
	ID      *RequestID      `json:"id,omitempty"`     // Null for notifications
	Method  string          `json:"method"`           // Method name
	Params  json.RawMessage `json:"params,omitempty"` // Method-specific params
}

Request represents a JSON-RPC 2.0 request

type RequestID

type RequestID struct {
	Str *string
	Num *int64
}

RequestID can be string, number, or null per JSON-RPC 2.0 spec

func NewNumericRequestID

func NewNumericRequestID(n int64) *RequestID

NewNumericRequestID creates a RequestID from a number

func NewStringRequestID

func NewStringRequestID(s string) *RequestID

NewStringRequestID creates a RequestID from a string

func (*RequestID) MarshalJSON

func (r *RequestID) MarshalJSON() ([]byte, error)

MarshalJSON implements json.Marshaler for RequestID

func (*RequestID) String

func (r *RequestID) String() string

String returns a string representation of the RequestID

func (*RequestID) UnmarshalJSON

func (r *RequestID) UnmarshalJSON(data []byte) error

UnmarshalJSON implements json.Unmarshaler for RequestID

type Resource

type Resource struct {
	URI         string `json:"uri"`
	Name        string `json:"name"`
	Description string `json:"description,omitempty"`
	MimeType    string `json:"mimeType,omitempty"`
}

Resource represents an MCP resource definition

type ResourceContents

type ResourceContents struct {
	URI      string `json:"uri"`
	MimeType string `json:"mimeType,omitempty"`
	Text     string `json:"text,omitempty"`
	Blob     string `json:"blob,omitempty"` // Base64
}

ResourceContents contains resource data

type ResourceListChangedNotification

type ResourceListChangedNotification struct{}

ResourceListChangedNotification notifies that the resource list has changed

type ResourceListResult

type ResourceListResult struct {
	Resources []Resource `json:"resources"`
}

ResourceListResult is the response from resources/list

type ResourceRef

type ResourceRef struct {
	URI      string `json:"uri"`
	MimeType string `json:"mimeType,omitempty"`
}

ResourceRef references a resource

type ResourceTemplate

type ResourceTemplate struct {
	URITemplate string `json:"uriTemplate"`
	Name        string `json:"name"`
	Description string `json:"description,omitempty"`
	MimeType    string `json:"mimeType,omitempty"`
}

ResourceTemplate defines a dynamic resource URI template

type ResourceUpdatedNotification

type ResourceUpdatedNotification struct {
	URI string `json:"uri"`
}

ResourceUpdatedNotification notifies of a resource change

type ResourcesCapability

type ResourcesCapability struct {
	Subscribe   bool `json:"subscribe,omitempty"`   // Supports subscriptions
	ListChanged bool `json:"listChanged,omitempty"` // Sends list change notifications
}

type Response

type Response struct {
	JSONRPC string          `json:"jsonrpc"`          // Must be "2.0"
	ID      *RequestID      `json:"id"`               // Must match request
	Result  json.RawMessage `json:"result,omitempty"` // Success result
	Error   *Error          `json:"error,omitempty"`  // Error (mutually exclusive with Result)
}

Response represents a JSON-RPC 2.0 response

type RootsCapability

type RootsCapability struct{}

Capability markers (empty structs indicate support)

type SamplingCapability

type SamplingCapability struct{}

type SamplingParams

type SamplingParams struct {
	Messages       []PromptMessage        `json:"messages"`
	ModelPrefs     *ModelPreferences      `json:"modelPreferences,omitempty"`
	SystemPrompt   string                 `json:"systemPrompt,omitempty"`
	IncludeContext string                 `json:"includeContext,omitempty"` // "none", "thisServer", "allServers"
	Temperature    *float64               `json:"temperature,omitempty"`
	MaxTokens      int                    `json:"maxTokens"`
	StopSequences  []string               `json:"stopSequences,omitempty"`
	Metadata       map[string]interface{} `json:"metadata,omitempty"`
}

SamplingParams contains parameters for sampling/createMessage

type SamplingResult

type SamplingResult struct {
	Role       string  `json:"role"` // "assistant"
	Content    Content `json:"content"`
	Model      string  `json:"model"`
	StopReason string  `json:"stopReason,omitempty"` // "endTurn", "stopSequence", "maxTokens"
}

SamplingResult is the response from sampling/createMessage

type ServerCapabilities

type ServerCapabilities struct {
	Tools     *ToolsCapability     `json:"tools,omitempty"`
	Resources *ResourcesCapability `json:"resources,omitempty"`
	Prompts   *PromptsCapability   `json:"prompts,omitempty"`
	Logging   *LoggingCapability   `json:"logging,omitempty"`
}

ServerCapabilities declares what the server supports

type Tool

type Tool struct {
	Name        string                 `json:"name"`
	Description string                 `json:"description"`
	InputSchema map[string]interface{} `json:"inputSchema"` // JSON Schema
}

Tool represents an MCP tool definition

type ToolListResult

type ToolListResult struct {
	Tools []Tool `json:"tools"`
}

ToolListResult is the response from tools/list

type ToolsCapability

type ToolsCapability struct{}

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL