Documentation
¶
Overview ¶
Package resource provides resource management functionality.
Package resource is a generated GoMock package.
Index ¶
- Variables
- type DynamicResource
- type Manager
- func (rm *Manager) AddResource(resource Resource)
- func (rm *Manager) ClearResourcesForService(serviceID string)
- func (rm *Manager) GetResource(uri string) (Resource, bool)
- func (rm *Manager) ListResources() []Resource
- func (rm *Manager) OnListChanged(f func())
- func (rm *Manager) RemoveResource(uri string)
- func (rm *Manager) Subscribe(ctx context.Context, uri string) error
- type ManagerInterface
- type MockManagerInterface
- func (m *MockManagerInterface) AddResource(resource Resource)
- func (m *MockManagerInterface) Clear()
- func (m *MockManagerInterface) ClearResourcesForService(serviceID string)
- func (m *MockManagerInterface) EXPECT() *MockManagerInterfaceMockRecorder
- func (m *MockManagerInterface) GetResource(uri string) (Resource, bool)
- func (m *MockManagerInterface) ListResources() []Resource
- func (m *MockManagerInterface) OnListChanged(arg0 func())
- func (m *MockManagerInterface) RemoveResource(uri string)
- type MockManagerInterfaceMockRecorder
- func (mr *MockManagerInterfaceMockRecorder) AddResource(resource any) *gomock.Call
- func (mr *MockManagerInterfaceMockRecorder) Clear() *gomock.Call
- func (mr *MockManagerInterfaceMockRecorder) ClearResourcesForService(serviceID any) *gomock.Call
- func (mr *MockManagerInterfaceMockRecorder) GetResource(uri any) *gomock.Call
- func (mr *MockManagerInterfaceMockRecorder) ListResources() *gomock.Call
- func (mr *MockManagerInterfaceMockRecorder) OnListChanged(arg0 any) *gomock.Call
- func (mr *MockManagerInterfaceMockRecorder) RemoveResource(uri any) *gomock.Call
- type Resource
- type StaticResource
Constants ¶
This section is empty.
Variables ¶
var ErrResourceNotFound = errors.New("resource not found")
ErrResourceNotFound is returned when a requested resource cannot be found.
Functions ¶
This section is empty.
Types ¶
type DynamicResource ¶
type DynamicResource struct {
// contains filtered or unexported fields
}
DynamicResource implements the Resource interface for resources that are fetched dynamically by executing a tool.
func NewDynamicResource ¶
func NewDynamicResource(def *configv1.ResourceDefinition, t tool.Tool) (*DynamicResource, error)
NewDynamicResource creates a new instance of DynamicResource.
Summary: Initializes a dynamic resource backed by a tool.
Parameters:
- def: *configv1.ResourceDefinition. The resource definition.
- t: tool.Tool. The tool used to fetch the resource content.
Returns:
- *DynamicResource: The initialized dynamic resource.
- error: An error if validation fails.
func (*DynamicResource) Read ¶
func (r *DynamicResource) Read(ctx context.Context) (*mcp.ReadResourceResult, error)
Read executes the associated tool to fetch the resource content.
Summary: Fetches the resource content by executing the tool.
Parameters:
- ctx: context.Context. The context for the request.
Returns:
- *mcp.ReadResourceResult: The resource content.
- error: An error if the tool execution fails.
Side Effects:
- Executes the underlying tool, which may have its own side effects.
func (*DynamicResource) Resource ¶
func (r *DynamicResource) Resource() *mcp.Resource
Resource returns the MCP representation of the resource.
Summary: Retrieves the MCP resource metadata.
Returns:
- *mcp.Resource: The MCP resource definition.
func (*DynamicResource) Service ¶
func (r *DynamicResource) Service() string
Service returns the ID of the service that provides this resource.
Summary: Retrieves the service ID.
Returns:
- string: The service ID.
func (*DynamicResource) Subscribe ¶
func (r *DynamicResource) Subscribe(_ context.Context) error
Subscribe is not yet implemented for dynamic resources.
Summary: Subscribes to resource updates (Not Implemented).
Parameters:
- _: context.Context. Unused.
Returns:
- error: Always returns an error indicating not implemented.
type Manager ¶
type Manager struct {
// contains filtered or unexported fields
}
Manager is a thread-safe implementation of the ManagerInterface.
It manages the lifecycle and retrieval of resources, providing thread-safe access and efficient listing via caching.
func NewManager ¶
func NewManager() *Manager
NewManager creates and returns a new, empty Manager.
Returns:
- *Manager: A new Manager instance.
func (*Manager) AddResource ¶
AddResource adds a new resource to the manager.
Parameters:
- resource: Resource. The resource to add.
Side Effects:
- Updates the internal resource storage.
- Invalidates the list cache.
- Triggers the on-change callback if registered.
func (*Manager) ClearResourcesForService ¶
ClearResourcesForService removes all resources associated with a given service ID.
Parameters:
- serviceID: string. The service ID.
Side Effects:
- Removes matching resources from storage.
- Invalidates the list cache.
- Triggers the on-change callback.
func (*Manager) GetResource ¶
GetResource retrieves a resource from the manager by its URI.
Parameters:
- uri: string. The URI of the resource.
Returns:
- Resource: The resource instance.
- bool: True if found, false otherwise.
func (*Manager) ListResources ¶
ListResources returns a slice containing all the resources currently registered in the manager.
It uses a read-through cache (double-checked locking) to minimize allocation overhead for frequent calls.
Returns:
- []Resource: A slice of currently registered resources.
func (*Manager) OnListChanged ¶
func (rm *Manager) OnListChanged(f func())
OnListChanged sets a callback function that will be invoked whenever the list of resources is modified.
Parameters:
- f: func(). The callback function.
func (*Manager) RemoveResource ¶
RemoveResource removes a resource from the manager by its URI.
Parameters:
- uri: string. The URI of the resource.
Side Effects:
- Updates the internal resource storage.
- Invalidates the list cache.
- Triggers the on-change callback if registered.
type ManagerInterface ¶
type ManagerInterface interface {
// GetResource retrieves a resource by its URI.
//
// Parameters:
// - uri: string. The URI of the resource.
//
// Returns:
// - Resource: The resource instance.
// - bool: True if found, false otherwise.
GetResource(uri string) (Resource, bool)
// AddResource adds a new resource to the manager.
//
// Parameters:
// - resource: Resource. The resource to add.
AddResource(resource Resource)
// RemoveResource removes a resource from the manager by its URI.
//
// Parameters:
// - uri: string. The URI of the resource to remove.
RemoveResource(uri string)
// ListResources returns a slice of all resources currently in the manager.
//
// Returns:
// - []Resource: A slice of resources.
ListResources() []Resource
// OnListChanged registers a callback function to be called when the list of resources changes.
//
// Parameters:
// - f: func(). The callback function to execute on change.
OnListChanged(f func())
// ClearResourcesForService removes all resources associated with a given service ID.
//
// Parameters:
// - serviceID: string. The service ID.
ClearResourcesForService(serviceID string)
}
ManagerInterface defines the interface for managing a collection of resources.
It provides methods for adding, removing, listing, and retrieving resources, as well as managing callbacks for list changes.
type MockManagerInterface ¶
type MockManagerInterface struct {
// contains filtered or unexported fields
}
MockManagerInterface is a mock of ManagerInterface interface.
func NewMockManagerInterface ¶
func NewMockManagerInterface(ctrl *gomock.Controller) *MockManagerInterface
NewMockManagerInterface creates a new mock instance.
ctrl is the ctrl.
Returns the result.
func (*MockManagerInterface) AddResource ¶
func (m *MockManagerInterface) AddResource(resource Resource)
AddResource mocks base method.
resource is the resource.
func (*MockManagerInterface) Clear ¶
func (m *MockManagerInterface) Clear()
Clear mocks base method.
func (*MockManagerInterface) ClearResourcesForService ¶
func (m *MockManagerInterface) ClearResourcesForService(serviceID string)
ClearResourcesForService mocks base method.
serviceID is the serviceID.
func (*MockManagerInterface) EXPECT ¶
func (m *MockManagerInterface) EXPECT() *MockManagerInterfaceMockRecorder
EXPECT returns an object that allows the caller to indicate expected use.
Returns the result.
func (*MockManagerInterface) GetResource ¶
func (m *MockManagerInterface) GetResource(uri string) (Resource, bool)
GetResource mocks base method.
uri is the uri.
Returns the result. Returns true if successful.
func (*MockManagerInterface) ListResources ¶
func (m *MockManagerInterface) ListResources() []Resource
ListResources mocks base method.
Returns the result.
func (*MockManagerInterface) OnListChanged ¶
func (m *MockManagerInterface) OnListChanged(arg0 func())
OnListChanged mocks base method.
arg0 is the arg0.
func (*MockManagerInterface) RemoveResource ¶
func (m *MockManagerInterface) RemoveResource(uri string)
RemoveResource mocks base method.
uri is the uri.
type MockManagerInterfaceMockRecorder ¶
type MockManagerInterfaceMockRecorder struct {
// contains filtered or unexported fields
}
MockManagerInterfaceMockRecorder is the mock recorder for MockManagerInterface.
func (*MockManagerInterfaceMockRecorder) AddResource ¶
func (mr *MockManagerInterfaceMockRecorder) AddResource(resource any) *gomock.Call
AddResource indicates an expected call of AddResource.
resource is the resource.
Returns the result.
func (*MockManagerInterfaceMockRecorder) Clear ¶
func (mr *MockManagerInterfaceMockRecorder) Clear() *gomock.Call
Clear indicates an expected call of Clear.
Returns the result.
func (*MockManagerInterfaceMockRecorder) ClearResourcesForService ¶
func (mr *MockManagerInterfaceMockRecorder) ClearResourcesForService(serviceID any) *gomock.Call
ClearResourcesForService indicates an expected call of ClearResourcesForService.
serviceID is the serviceID.
Returns the result.
func (*MockManagerInterfaceMockRecorder) GetResource ¶
func (mr *MockManagerInterfaceMockRecorder) GetResource(uri any) *gomock.Call
GetResource indicates an expected call of GetResource.
uri is the uri.
Returns the result.
func (*MockManagerInterfaceMockRecorder) ListResources ¶
func (mr *MockManagerInterfaceMockRecorder) ListResources() *gomock.Call
ListResources indicates an expected call of ListResources.
Returns the result.
func (*MockManagerInterfaceMockRecorder) OnListChanged ¶
func (mr *MockManagerInterfaceMockRecorder) OnListChanged(arg0 any) *gomock.Call
OnListChanged indicates an expected call of OnListChanged.
arg0 is the arg0.
Returns the result.
func (*MockManagerInterfaceMockRecorder) RemoveResource ¶
func (mr *MockManagerInterfaceMockRecorder) RemoveResource(uri any) *gomock.Call
RemoveResource indicates an expected call of RemoveResource.
uri is the uri.
Returns the result.
type Resource ¶
type Resource interface {
// Resource returns the MCP representation of the resource, which includes its metadata.
//
// Returns:
// - *mcp.Resource: The MCP resource definition.
Resource() *mcp.Resource
// Service returns the ID of the service that provides this resource.
//
// Returns:
// - string: The service ID.
Service() string
// Read retrieves the content of the resource.
//
// Parameters:
// - ctx: context.Context. The context for the request.
//
// Returns:
// - *mcp.ReadResourceResult: The content of the resource.
// - error: An error if reading fails.
Read(ctx context.Context) (*mcp.ReadResourceResult, error)
// Subscribe establishes a subscription to the resource, allowing for receiving updates.
//
// Parameters:
// - ctx: context.Context. The context for the subscription.
//
// Returns:
// - error: An error if subscription fails.
Subscribe(ctx context.Context) error
}
Resource defines the interface for a resource that can be managed by the Manager.
A resource represents a data source (e.g., a file, a database record) that can be read by an MCP client.
type StaticResource ¶
type StaticResource struct {
// contains filtered or unexported fields
}
StaticResource implements the Resource interface for resources that are defined statically in the configuration (e.g. pointing to a URL).
func NewStaticResource ¶
func NewStaticResource(def *configv1.ResourceDefinition, serviceID string) *StaticResource
NewStaticResource creates a new instance of StaticResource.
Summary: Initializes a static resource.
Parameters:
- def: *configv1.ResourceDefinition. The resource definition.
- serviceID: string. The ID of the service owning the resource.
Returns:
- *StaticResource: The initialized static resource.
func (*StaticResource) Read ¶
func (r *StaticResource) Read(ctx context.Context) (*mcp.ReadResourceResult, error)
Read retrieves the content of the resource by fetching the URI.
Summary: Fetches the resource content.
Parameters:
- ctx: context.Context. The context for the request.
Returns:
- *mcp.ReadResourceResult: The resource content.
- error: An error if fetching fails.
Side Effects:
- Performs an HTTP GET request to the resource URI (if not inline content).
func (*StaticResource) Resource ¶
func (r *StaticResource) Resource() *mcp.Resource
Resource returns the MCP representation of the resource.
Summary: Retrieves the MCP resource metadata.
Returns:
- *mcp.Resource: The MCP resource definition.
func (*StaticResource) Service ¶
func (r *StaticResource) Service() string
Service returns the ID of the service that provides this resource.
Summary: Retrieves the service ID.
Returns:
- string: The service ID.
func (*StaticResource) Subscribe ¶
func (r *StaticResource) Subscribe(_ context.Context) error
Subscribe is not yet implemented for static resources.
Summary: Subscribes to resource updates (Not Implemented).
Parameters:
- _: context.Context. Unused.
Returns:
- error: Always returns an error indicating not implemented.