fix: mcp serve

This commit is contained in:
2026-03-18 15:19:55 +08:00
parent 5389345a00
commit e5ce8c20b5
2 changed files with 78 additions and 16 deletions
+52 -8
View File
@@ -47,7 +47,7 @@ function getMimeType(filePath: string): string {
/**
* 扫描目录内的 .md 文件,生成索引
*/
function scanDirectory(dir: string): ContentIndex {
export function scanDirectory(dir: string): ContentIndex {
const index: ContentIndex = {};
function scan(currentPath: string, relativePath: string) {
@@ -176,10 +176,35 @@ function createRequestHandler(
}
/**
* 启动开发服务器
* 内容服务器接口
*/
export const serveCommand: ServeCommandHandler = async (dir, options) => {
const contentDir = resolve(dir);
export interface ContentServer {
/**
* HTTP 服务器实例
*/
server: Server;
/**
* 文件监听器
*/
watcher: ReturnType<typeof watch>;
/**
* 内容索引
*/
index: ContentIndex;
/**
* 关闭服务器
*/
close(): void;
}
/**
* 创建内容服务器
*/
export function createContentServer(
contentDir: string,
port: number,
distPath: string = distDir,
): ContentServer {
let contentIndex: ContentIndex = {};
// 扫描内容目录生成索引
@@ -231,19 +256,38 @@ export const serveCommand: ServeCommandHandler = async (dir, options) => {
// 创建请求处理器
const handleRequest = createRequestHandler(
contentDir,
distDir,
distPath,
() => contentIndex,
);
// 创建 HTTP 服务器
const server = createServer(handleRequest);
const port = parseInt(options.port, 10);
server.listen(port, () => {
console.log(`\n开发服务器已启动:http://localhost:${port}`);
console.log(`内容目录:${contentDir}`);
console.log(`静态资源目录:${distDir}`);
console.log(`静态资源目录:${distPath}`);
console.log(`索引文件:http://localhost:${port}/__CONTENT_INDEX.json\n`);
});
return {
server,
watcher,
index: contentIndex,
close() {
console.log("关闭内容服务器...");
server.close();
watcher.close();
},
};
}
/**
* 启动开发服务器
*/
export const serveCommand: ServeCommandHandler = async (dir, options) => {
const contentDir = resolve(dir);
const port = parseInt(options.port, 10);
createContentServer(contentDir, port);
};