Documentation
¶
Overview ¶
Package webfetch is a dependency-light Go port of the reference Python "mcp-server-fetch" tool (github.com/modelcontextprotocol/servers, src/fetch). It fetches a URL, optionally extracts the page's main content as Markdown, and returns text ready to hand to an LLM.
The observable contract of the upstream tool is reproduced closely: the autonomous User-Agent string, the HTML/raw content-type heuristic, the "Contents of <url>:" wrapper, and the truncation / error strings.
The one unavoidable deviation is content extraction: upstream runs Mozilla Readability.js in a Node subprocess (readabilipy use_readability=True) plus Python markdownify. That JS pipeline cannot be reproduced byte-for-byte in pure Go, so we use codeberg.org/readeck/go-readability (a maintained Go port of the same Readability.js) followed by JohannesKaufmann/html-to-markdown configured to match markdownify's defaults (ATX headings, "*" bullets, "*" emphasis). On typical pages this is byte-identical to the Python output; the only observed difference is readability's URL normalization (e.g. a trailing slash added to bare links). Staying in-process (no Node, no subprocess) is also what makes the sidecar container removable, which is the point of this package.
Index ¶
Constants ¶
const DefaultUserAgentAutonomous = "ModelContextProtocol/1.0 (Autonomous; +https://github.com/modelcontextprotocol/servers)"
DefaultUserAgentAutonomous is the User-Agent sent for autonomous (tool-driven) fetches. It is copied verbatim from upstream mcp-server-fetch; the reference server presents this generic identity rather than the real client, and we preserve that behaviour intentionally.
Variables ¶
This section is empty.
Functions ¶
func Fetch ¶
Fetch fetches the URL, extracts/keeps the content, applies start_index/max_length paging, and returns the text wrapped as "<prefix>Contents of <url>:\n<content>". Outbound connections are restricted to public IPs by the SSRF guard in the dialer.
It returns a non-nil error on connection failure or HTTP status >= 400. Callers that have an alternate reader (e.g. a headless-browser fallback) should treat a non-nil error as "try the fallback".
Types ¶
type Options ¶
type Options struct {
// MaxLength is the maximum number of characters to return. Zero means the
// upstream default (5000).
MaxLength int
// StartIndex returns output starting at this character index, for paging a
// previously truncated fetch.
StartIndex int
// Raw returns the actual HTML without Markdown simplification.
Raw bool
// UserAgent overrides the autonomous User-Agent. Empty uses the default.
UserAgent string
// IncludeMetadata, when true, prepends a small YAML frontmatter block
// (title, author, published, site, language — non-empty fields only) ahead
// of the extracted Markdown. It applies only to the HTML-simplification path
// (not Raw and not non-HTML content). Default false, which keeps the output
// byte-identical to upstream mcp-server-fetch.
//
// The frontmatter counts as part of the returned content, so StartIndex /
// MaxLength page over it too; hold IncludeMetadata constant across paged
// calls so a page-2 StartIndex stays aligned.
IncludeMetadata bool
// ExtractPDF, when true, extracts the text of PDF responses (detected by
// content-type or the "%PDF-" magic bytes) instead of returning the raw
// bytes behind the "cannot be simplified" note. Extraction is pure-Go (no
// subprocess). Raw takes precedence: if Raw is set, the PDF is returned
// unextracted. Default false, preserving the upstream raw-bytes behaviour.
ExtractPDF bool
// FullPage converts the entire page to Markdown, skipping the Readability
// main-content extraction. Use it when Readability over-strips (docs pages,
// tables, sidebars you actually want). Ignored when Selector is set, and when
// Raw is set. IncludeMetadata is not applied on this path. Default false.
FullPage bool
// Selector, when set, converts only the element(s) matching this CSS selector
// to Markdown, skipping Readability (an escape hatch for targeting a specific
// region). Takes precedence over FullPage. Ignored when Raw is set.
// IncludeMetadata is not applied on this path. If nothing matches, the content
// is the "<error>No content matched the selector.</error>" sentinel (with a
// nil error). Default "".
Selector string
// ExcludeSelectors removes element(s) matching these CSS selectors before
// conversion. Unlike FullPage/Selector it composes with every non-raw mode,
// including the default Readability path (e.g. strip a cookie banner, then
// simplify). Empty (the default) leaves the input untouched, so output stays
// byte-identical to upstream. Ignored when Raw is set.
ExcludeSelectors []string
}
Options mirror the upstream tool's parameters.