mcp

package
v1.0.3 Latest Latest
Warning

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

Go to latest
Published: Jul 10, 2026 License: MPL-2.0 Imports: 14 Imported by: 0

Documentation

Overview

Package mcp exposes codamigo's search capability as an MCP (Model Context Protocol) stdio server.

Server is constructed with a *query.Querier and optional *indexer.Indexer and watcher.Watcher. Server.Serve runs the MCP stdio loop until the context is cancelled. The server exposes a "search" tool for semantic code search and a "get_map" tool that returns a structural map of the codebase. When no Indexer is provided, refresh_index=true is silently ignored.

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type MapInput added in v1.0.1

type MapInput struct {
	MaxTokens      *int  `json:"max_tokens,omitempty" jsonschema:"Maximum token budget for the response (default 2000)"`
	CodeOnly       *bool `json:"code_only,omitempty" jsonschema:"Exclude configured non-code languages from the map (default true)"`
	ShowSummary    *bool `json:"show_summary,omitempty" jsonschema:"Show per-file type summary in file headers (default true)"`
	ShowVisibility *bool `json:"show_visibility,omitempty" jsonschema:"Show export markers: + for public, - for internal (default true)"`
}

MapInput defines the parameters for the get_map MCP tool. Pointer fields are used where defaults differ from Go zero values.

type SearchInput added in v1.0.1

type SearchInput struct {
	Query        string   `json:"query" jsonschema:"Search text to embed and match against indexed chunks"`
	Limit        int      `json:"limit,omitempty" jsonschema:"Maximum number of results to return (default 10)"`
	Languages    []string `json:"languages,omitempty" jsonschema:"Filter results by programming language (e.g. [\"go\", \"python\"])"`
	Paths        []string `json:"paths,omitempty" jsonschema:"Glob patterns to restrict search scope (e.g. [\"cmd/**\"])"`
	MaxTokens    int      `json:"max_tokens,omitempty" jsonschema:"Token budget for results. 0 = no limit (default 0)"`
	Package      string   `json:"package,omitempty" jsonschema:"Filter results to a package (e.g. \"store\", \"cmd/codamigo\")"`
	RefreshIndex bool     `json:"refresh_index,omitempty" jsonschema:"Force a full re-index before querying (default false)"`
	Name         string   `json:"name,omitempty" jsonschema:"Filter results to chunks matching this symbol name (e.g. \"Search\", \"NewChunker\")"`
	NodeKinds    []string `json:"node_kinds,omitempty" jsonschema:"Filter by AST node kind (e.g. [\"function_declaration\", \"type_declaration\"])"`
	MetadataOnly bool     `` /* 180-byte string literal not displayed */
	Offset       int      `json:"offset,omitempty" jsonschema:"Number of results to skip for pagination (default 0)"`
}

SearchInput defines the parameters for the search MCP tool. The SDK infers the JSON schema from this struct's json and jsonschema tags.

type Server

type Server struct {
	// contains filtered or unexported fields
}

Server is the MCP stdio server for codamigo.

func NewServer

func NewServer(q *query.Querier, idx *indexer.Indexer, w watcher.Watcher, nonCodeLangs []string) *Server

NewServer constructs the MCP server. All dependencies are optional (may be nil); the server degrades gracefully. When idx is non-nil, a search request with refresh_index=true triggers a full re-index before querying. When w is non-nil, changed files are re-indexed continuously in the background. nonCodeLangs configures which languages are excluded when the get_map code_only option is true.

func NewServerWithIndexer

func NewServerWithIndexer(q *query.Querier, idx indexerIface, w watcher.Watcher, nonCodeLangs []string) *Server

NewServerWithIndexer is like NewServer but accepts any value that satisfies [indexerIface]. This is useful in tests where a lightweight mock replaces the real *indexer.Indexer.

func (*Server) HandleMap

func (s *Server) HandleMap(ctx context.Context, req *mcp.CallToolRequest, input MapInput) (*mcp.CallToolResult, any, error)

HandleMap is the tool handler for the get_map MCP tool. Exported to allow direct testing without the MCP stdio transport.

func (*Server) HandleSearch

func (s *Server) HandleSearch(ctx context.Context, req *mcp.CallToolRequest, input SearchInput) (*mcp.CallToolResult, any, error)

HandleSearch is the tool handler for the search MCP tool. Exported to allow direct testing without the MCP stdio transport.

func (*Server) Serve

func (s *Server) Serve(ctx context.Context) error

Serve runs the MCP stdio loop over os.Stdin and os.Stdout until ctx is cancelled. See ServeIO for control over the transport streams.

func (*Server) ServeIO

func (s *Server) ServeIO(ctx context.Context, in io.Reader, out io.Writer) error

ServeIO runs the MCP stdio loop over the provided streams until ctx is cancelled. It performs an initial full index (if indexer is non-nil), launches a background watcher goroutine (if both watcher and indexer are non-nil), and then blocks on the MCP stdio protocol loop. All goroutines are joined before ServeIO returns.

Jump to

Keyboard shortcuts

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