typescript-sdk/docs/.vitepress/config.mts

77 lines
3.5 KiB
TypeScript

import { existsSync, readFileSync } from 'node:fs';
import { dirname, resolve } from 'node:path';
import { fileURLToPath } from 'node:url';
import { defineConfig, type DefaultTheme } from 'vitepress';
import { generateLlmsArtifacts } from './llms';
import { guideSidebar } from './nav';
const docsDir = resolve(dirname(fileURLToPath(import.meta.url)), '..');
/** Absolute site URL (origin + base), the single copy every consumer derives from. */
const siteUrl = 'https://ts.sdk.modelcontextprotocol.io/v2';
/**
* The API Reference sidebar is generated by `pnpm docs:api` (typedoc + typedoc-vitepress-theme)
* into docs/api/typedoc-sidebar.json. When it is missing (e.g. `docs:dev` before `docs:api`),
* fall back to an empty group with a hint instead of crashing.
*/
function apiSidebarItems(): DefaultTheme.SidebarItem[] {
const sidebarPath = resolve(docsDir, 'api/typedoc-sidebar.json');
if (!existsSync(sidebarPath)) {
console.warn(`[docs] ${sidebarPath} not found — run \`pnpm docs:api\` first to populate the API Reference section.`);
return [];
}
return JSON.parse(readFileSync(sidebarPath, 'utf8'));
}
export default defineConfig({
title: 'MCP TypeScript SDK',
description: 'The TypeScript SDK implementation of the Model Context Protocol specification.',
base: '/v2/',
// VitePress does not base-prefix head hrefs, so /v2/ is spelled out here.
head: [['link', { rel: 'icon', type: 'image/svg+xml', href: '/v2/favicon.svg' }]],
srcExclude: ['v1/**', '_meta/**', 'behavior-surface-pins.md'],
sitemap: { hostname: `${siteUrl}/` },
markdown: {
config(md) {
// Spec-generated JSDoc (packages/core-internal/src/types/spec.types.*.ts) carries
// site-root-relative links like /specification/draft/basic/index#meta that are meant
// to resolve on modelcontextprotocol.io. Rewrite them at render time so they work on
// this site (and so the dead-link check sees them as external).
const orig = md.renderer.rules.link_open ?? ((tokens, idx, options, _env, self) => self.renderToken(tokens, idx, options));
md.renderer.rules.link_open = (tokens, idx, options, env, self) => {
const href = tokens[idx].attrGet('href');
if (href?.startsWith('/specification/')) {
tokens[idx].attrSet('href', `https://modelcontextprotocol.io${href}`);
}
return orig(tokens, idx, options, env, self);
};
}
},
buildEnd(siteConfig) {
generateLlmsArtifacts(docsDir, siteConfig.outDir, siteUrl);
},
themeConfig: {
nav: [
{ text: 'Get started', link: '/get-started/first-server', activeMatch: '^/get-started/' },
{ text: 'Servers', link: '/servers/tools', activeMatch: '^/(servers|serving)/' },
{ text: 'Clients', link: '/clients/connect', activeMatch: '^/clients/' },
{ text: 'Migration', link: '/migration/', activeMatch: '^/migration/' },
{ text: 'API Reference', link: '/api/', activeMatch: '^/api/' },
{ text: 'V1 Docs', link: 'https://ts.sdk.modelcontextprotocol.io/' }
],
sidebar: [
...guideSidebar,
{
text: 'API Reference',
collapsed: true,
items: apiSidebarItems()
}
],
outline: { level: [2, 3] },
search: { provider: 'local' },
socialLinks: [{ icon: 'github', link: 'https://github.com/modelcontextprotocol/typescript-sdk' }]
}
});