@mokei/mcp-fetch
Version:
Fetch MCP server
106 lines (105 loc) • 3.48 kB
JavaScript
import { createTool } from '@mokei/context-server';
import Turndown from 'turndown';
// @ts-expect-error no types
import { gfm } from 'turndown-plugin-gfm';
/**
* Create a default Turndown service instance with GFM support.
*/ export function createTurndownService() {
return new Turndown().use(gfm).remove([
'script',
'style'
]);
}
/**
* Create fetch tool definitions.
*
* @example
* ```typescript
* import { createFetchTools } from '@mokei/mcp-fetch'
*
* const tools = createFetchTools()
* ```
*/ export function createFetchTools(options = {}) {
const turndownService = options.turndownService ?? createTurndownService();
return {
get_markdown: createTool({
description: 'Fetch a URL and return its contents as markdown',
inputSchema: {
type: 'object',
properties: {
url: {
type: 'string',
format: 'uri',
description: 'HTTP URL to fetch contents from'
}
},
required: [
'url'
],
additionalProperties: false
},
handler: async (req)=>{
try {
const res = await fetch(req.input.url);
if (!res.ok) {
return {
content: [
{
type: 'text',
text: `Failed to fetch with response status ${res.status}: ${res.statusText}`
}
],
isError: true
};
}
const text = await res.text();
const markdown = turndownService.turndown(text);
return {
content: [
{
type: 'text',
text: markdown
}
],
isError: false
};
} catch (err) {
return {
content: [
{
type: 'text',
text: err.message ?? 'Unknown error'
}
],
isError: true
};
}
}
})
};
}
/**
* Create a server config for the fetch MCP server.
*
* @example
* ```typescript
* import { createFetchConfig } from '@mokei/mcp-fetch'
* import { ContextServer } from '@mokei/context-server'
*
* const config = createFetchConfig()
* const server = new ContextServer({ ...config, transport })
* ```
*/ export function createFetchConfig(options = {}) {
return {
name: 'fetch',
version: '0.1.0',
// Both revisions: these ship with mokei and are what its host and CLI point at by
// default, so they must answer a `2026-07-28` client — while still reaching every
// client that only speaks `2025-11-25`, including the current SDK release.
protocolVersions: [
'2026-07-28',
'2025-11-25'
],
tools: createFetchTools(options)
};
}