mcp and content fix
This commit is contained in:
@@ -5,6 +5,7 @@ import type { I18nConfig } from '../i18n/index.js'
|
||||
|
||||
import { createMarkdownRenderer, searchContent } from '../content/index.js'
|
||||
import { getDefaultLocale, isValidLocale } from '../i18n/index.js'
|
||||
import { checkRateLimit, clientIp, tooManyRequests } from '../shared/rateLimit.js'
|
||||
|
||||
/**
|
||||
* A read-only MCP (Model Context Protocol) server for the client site, as a Next
|
||||
@@ -46,6 +47,13 @@ type CreateMcpRouteArgs = {
|
||||
/** Archive-backed collections (same as the plugin option) for get_page. */
|
||||
content?: ContentOption
|
||||
getCachedPayload: () => Promise<BasePayload>
|
||||
/** Max messages in one JSON-RPC batch. Default 10. */
|
||||
maxBatch?: number
|
||||
/**
|
||||
* Per-IP limit on JSON-RPC messages (a batch counts every message) (in memory, per instance). Default { max: 60, windowMs: 60_000 }.
|
||||
* `false` = off (a limiter in front, e.g. Cloudflare WAF rate limiting).
|
||||
*/
|
||||
rateLimit?: { max?: number; windowMs?: number } | false
|
||||
/** Server name advertised in `initialize`. Default 'ipal-site'. */
|
||||
serverName?: string
|
||||
/** Server version advertised in `initialize`. Default '1.0.0'. */
|
||||
@@ -77,11 +85,13 @@ export function createMcpRoute(args: CreateMcpRouteArgs) {
|
||||
config,
|
||||
content,
|
||||
getCachedPayload,
|
||||
maxBatch = 10,
|
||||
serverName = 'ipal-site',
|
||||
serverVersion = '1.0.0',
|
||||
settingsSlug = 'site-settings',
|
||||
summaryField,
|
||||
} = args
|
||||
const rl = args.rateLimit === false ? null : { max: 60, windowMs: 60_000, ...args.rateLimit }
|
||||
|
||||
const origin = baseUrl?.replace(/\/$/, '') ?? ''
|
||||
const renderMarkdown = createMarkdownRenderer({ collection, content, settingsSlug })
|
||||
@@ -145,10 +155,7 @@ export function createMcpRoute(args: CreateMcpRouteArgs) {
|
||||
async function getPage(params: Record<string, unknown>): Promise<string> {
|
||||
const raw = typeof params.path === 'string' ? params.path.trim() : ''
|
||||
if (!raw) {return 'No path provided.'}
|
||||
const segments = raw
|
||||
.replace(/^\/+|\/+$/g, '')
|
||||
.split('/')
|
||||
.filter(Boolean)
|
||||
const segments = raw.replace(/^\/+|\/+$/g, '').split('/').filter(Boolean)
|
||||
let locale = getDefaultLocale(config)
|
||||
let slug = segments
|
||||
if (segments.length && isValidLocale(segments[0], config)) {
|
||||
@@ -210,6 +217,19 @@ export function createMcpRoute(args: CreateMcpRouteArgs) {
|
||||
return Response.json(rpcError(null, -32700, 'Parse error'), { status: 400 })
|
||||
}
|
||||
|
||||
// Limits: a batch counts as many requests as it has messages, and is capped —
|
||||
// otherwise one POST could carry hundreds of tool calls (DB queries).
|
||||
const messages = Array.isArray(body) ? body.length : 1
|
||||
if (Array.isArray(body) && body.length > maxBatch) {
|
||||
return Response.json(rpcError(null, -32600, `Batch too large (max ${maxBatch})`), { status: 400 })
|
||||
}
|
||||
if (rl) {
|
||||
const ip = clientIp(req)
|
||||
if (!checkRateLimit({ cost: messages, key: ip, scope: 'mcp', ...rl })) {
|
||||
return tooManyRequests(ip, 'mcp', rpcError(null, -32000, 'Too many requests'))
|
||||
}
|
||||
}
|
||||
|
||||
// Batch or single.
|
||||
if (Array.isArray(body)) {
|
||||
const out = (await Promise.all(body.map((m) => handleOne(m as JsonRpcRequest)))).filter(
|
||||
|
||||
Reference in New Issue
Block a user