Implementation:1broseidon Ketch mcp NewServer
| Knowledge Sources | |
|---|---|
| Domains | MCP, Server_Lifecycle |
| Last Updated | 2026-10-27 00:00 GMT |
Overview
Constructs the MCP server: builds the process-lifetime Scraper (shared by all concurrent tool calls), resolves the `mcp_tools` allowlist, and wires the page cache and tag index (both opened per operation).
Description
NewServer is the single constructor for the MCP server. It builds one `*scrape.Scraper` (compiled rewriter + lazy browser) via `scrape.NewFromConfig`, resolves the `mcp_tools` allowlist via `config.NormalizeMCPTools` (publishing all six tools when unset), constructs the page cache and the tag index, generates the server instructions from the enabled set, and registers each published tool. Process-lifetime resources are shared by all calls; the page cache and tag index open their bbolt files only per transaction (ADR-0006), so an idle server holds no lock.
Usage
Called by `ketch mcp serve`. The returned `*Server` is run with `Run` over a stdio transport and must be `Close`d on exit.
Code Reference
Source Location
- Repository: ketch
- File: mcp/server.go
Signature
type Server struct {
cfg *config.Config
tools []string
mcp *mcpsdk.Server
scraper *scrape.Scraper
tags *cache.Cache
cache *cache.Cache
}
// NewServer builds an MCP server named "ketch" exposing the six tools, backed
// by cfg. The returned error is a precondition failure (invalid url_rewrites
// or mcp_tools config).
func NewServer(cfg *config.Config, version string) (*Server, error)
Import
import (
"github.com/1broseidon/ketch/config"
"github.com/1broseidon/ketch/mcp"
)
I/O Contract
Inputs
| Name | Type | Required | Description |
|---|---|---|---|
| cfg | *config.Config | Yes | Operator config (backends, API keys, mcp_tools, browser) |
| version | string | Yes | CLI version (reported in the MCP server handshake) |
Outputs
| Name | Type | Description |
|---|---|---|
| return | *Server | Server with shared scraper + per-op cache/tags |
| return error | error | Invalid url_rewrites or mcp_tools (precondition) |
Usage Examples
Run Over Stdio
server, err := mcp.NewServer(cfg, version)
if err != nil {
return err // map to [precondition] / exit 5
}
defer server.Close()
err = server.Run(ctx, &mcpsdk.StdioTransport{})
Related Pages
Implements Principle
- Principle:1broseidon_Ketch_MCP_Tool_Allowlist
- Principle:1broseidon_Ketch_Per_Operation_Cache_Handles
- Principle:1broseidon_Ketch_MCP_Tool_Annotations