@weapp-vite/mcp 1.1.0 → 1.1.1

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/README.md CHANGED
@@ -11,7 +11,7 @@
11
11
  - 文档/变更记录资源暴露
12
12
  - 调试/改造提示词模板
13
13
 
14
- 默认通过 `stdio` 运行,适合接入任意 MCP Client
14
+ 默认通过 `stdio` 运行,适合接入任意 MCP Client,也支持 `streamable-http`。
15
15
 
16
16
  ## 启动
17
17
 
@@ -19,6 +19,22 @@
19
19
  pnpm --filter @weapp-vite/mcp start
20
20
  ```
21
21
 
22
+ 也可以在 Node 脚本里直接调用:
23
+
24
+ ```ts
25
+ import { startWeappViteMcpServer } from '@weapp-vite/mcp'
26
+
27
+ const handle = await startWeappViteMcpServer({
28
+ workspaceRoot: process.cwd(),
29
+ transport: 'streamable-http',
30
+ host: '127.0.0.1',
31
+ port: 3088,
32
+ endpoint: '/mcp',
33
+ })
34
+
35
+ await handle.close?.()
36
+ ```
37
+
22
38
  ## 主要 Tools
23
39
 
24
40
  - `workspace_catalog`: 输出 `weapp-vite / wevu / wevu-compiler` 目录、版本、脚本
package/dist/index.d.mts CHANGED
@@ -85,6 +85,25 @@ declare function createWeappViteMcpServer(options?: CreateServerOptions): Promis
85
85
  workspaceRoot: string;
86
86
  }>;
87
87
 
88
+ declare const DEFAULT_MCP_HOST = "127.0.0.1";
89
+ declare const DEFAULT_MCP_PORT = 3088;
90
+ declare const DEFAULT_MCP_ENDPOINT = "/mcp";
91
+ interface StartMcpServerOptions extends CreateServerOptions {
92
+ transport?: 'stdio' | 'streamable-http';
93
+ host?: string;
94
+ port?: number;
95
+ endpoint?: string;
96
+ unref?: boolean;
97
+ quiet?: boolean;
98
+ onReady?: (message: string) => void;
99
+ }
100
+ interface McpServerHandle {
101
+ transport: 'stdio' | 'streamable-http';
102
+ close?: () => Promise<void>;
103
+ }
104
+ declare function startStdioServer(options?: CreateServerOptions): Promise<void>;
105
+ declare function startWeappViteMcpServer(options?: StartMcpServerOptions): Promise<McpServerHandle>;
106
+
88
107
  declare function formatJson(value: unknown): string;
89
108
  declare function normalizeErrorMessage(error: unknown): string;
90
109
  declare function toToolResult(data: unknown, text?: string): {
@@ -108,7 +127,5 @@ declare function resolveWorkspaceRoot(start?: string): string;
108
127
  declare function assertInsideRoot(root: string, targetPath: string): string;
109
128
  declare function resolveSubPath(root: string, relativePath: string): string;
110
129
 
111
- declare function startStdioServer(): Promise<void>;
112
-
113
- export { DEFAULT_MAX_FILE_CHARS, DEFAULT_MAX_OUTPUT_CHARS, DEFAULT_MAX_RESULTS, DEFAULT_TIMEOUT_MS, EXPOSED_PACKAGES, MCP_SERVER_NAME, MCP_SERVER_VERSION, SKIPPED_DIR_NAMES, assertInsideRoot, createWeappViteMcpServer, formatJson, listFilesInDirectory, loadExposedCatalog, loadPackageSummary, normalizeErrorMessage, readFileContent, resolveSubPath, resolveWorkspaceRoot, runCommand, searchTextInDirectory, startStdioServer, toToolError, toToolResult };
114
- export type { CommandResult, CreateServerOptions, ExposedPackageId, ExposedPackageSummary, SearchMatch };
130
+ export { DEFAULT_MAX_FILE_CHARS, DEFAULT_MAX_OUTPUT_CHARS, DEFAULT_MAX_RESULTS, DEFAULT_MCP_ENDPOINT, DEFAULT_MCP_HOST, DEFAULT_MCP_PORT, DEFAULT_TIMEOUT_MS, EXPOSED_PACKAGES, MCP_SERVER_NAME, MCP_SERVER_VERSION, SKIPPED_DIR_NAMES, assertInsideRoot, createWeappViteMcpServer, formatJson, listFilesInDirectory, loadExposedCatalog, loadPackageSummary, normalizeErrorMessage, readFileContent, resolveSubPath, resolveWorkspaceRoot, runCommand, searchTextInDirectory, startStdioServer, startWeappViteMcpServer, toToolError, toToolResult };
131
+ export type { CommandResult, CreateServerOptions, ExposedPackageId, ExposedPackageSummary, McpServerHandle, SearchMatch, StartMcpServerOptions };
package/dist/index.d.ts CHANGED
@@ -85,6 +85,25 @@ declare function createWeappViteMcpServer(options?: CreateServerOptions): Promis
85
85
  workspaceRoot: string;
86
86
  }>;
87
87
 
88
+ declare const DEFAULT_MCP_HOST = "127.0.0.1";
89
+ declare const DEFAULT_MCP_PORT = 3088;
90
+ declare const DEFAULT_MCP_ENDPOINT = "/mcp";
91
+ interface StartMcpServerOptions extends CreateServerOptions {
92
+ transport?: 'stdio' | 'streamable-http';
93
+ host?: string;
94
+ port?: number;
95
+ endpoint?: string;
96
+ unref?: boolean;
97
+ quiet?: boolean;
98
+ onReady?: (message: string) => void;
99
+ }
100
+ interface McpServerHandle {
101
+ transport: 'stdio' | 'streamable-http';
102
+ close?: () => Promise<void>;
103
+ }
104
+ declare function startStdioServer(options?: CreateServerOptions): Promise<void>;
105
+ declare function startWeappViteMcpServer(options?: StartMcpServerOptions): Promise<McpServerHandle>;
106
+
88
107
  declare function formatJson(value: unknown): string;
89
108
  declare function normalizeErrorMessage(error: unknown): string;
90
109
  declare function toToolResult(data: unknown, text?: string): {
@@ -108,7 +127,5 @@ declare function resolveWorkspaceRoot(start?: string): string;
108
127
  declare function assertInsideRoot(root: string, targetPath: string): string;
109
128
  declare function resolveSubPath(root: string, relativePath: string): string;
110
129
 
111
- declare function startStdioServer(): Promise<void>;
112
-
113
- export { DEFAULT_MAX_FILE_CHARS, DEFAULT_MAX_OUTPUT_CHARS, DEFAULT_MAX_RESULTS, DEFAULT_TIMEOUT_MS, EXPOSED_PACKAGES, MCP_SERVER_NAME, MCP_SERVER_VERSION, SKIPPED_DIR_NAMES, assertInsideRoot, createWeappViteMcpServer, formatJson, listFilesInDirectory, loadExposedCatalog, loadPackageSummary, normalizeErrorMessage, readFileContent, resolveSubPath, resolveWorkspaceRoot, runCommand, searchTextInDirectory, startStdioServer, toToolError, toToolResult };
114
- export type { CommandResult, CreateServerOptions, ExposedPackageId, ExposedPackageSummary, SearchMatch };
130
+ export { DEFAULT_MAX_FILE_CHARS, DEFAULT_MAX_OUTPUT_CHARS, DEFAULT_MAX_RESULTS, DEFAULT_MCP_ENDPOINT, DEFAULT_MCP_HOST, DEFAULT_MCP_PORT, DEFAULT_TIMEOUT_MS, EXPOSED_PACKAGES, MCP_SERVER_NAME, MCP_SERVER_VERSION, SKIPPED_DIR_NAMES, assertInsideRoot, createWeappViteMcpServer, formatJson, listFilesInDirectory, loadExposedCatalog, loadPackageSummary, normalizeErrorMessage, readFileContent, resolveSubPath, resolveWorkspaceRoot, runCommand, searchTextInDirectory, startStdioServer, startWeappViteMcpServer, toToolError, toToolResult };
131
+ export type { CommandResult, CreateServerOptions, ExposedPackageId, ExposedPackageSummary, McpServerHandle, SearchMatch, StartMcpServerOptions };
package/dist/index.mjs CHANGED
@@ -1,12 +1,15 @@
1
1
  import process from 'node:process';
2
2
  import { fileURLToPath } from 'node:url';
3
- import { StdioServerTransport } from '@modelcontextprotocol/sdk/server/stdio.js';
4
3
  import fs$1 from 'node:fs/promises';
5
4
  import path from 'node:path';
6
- import { McpServer, ResourceTemplate } from '@modelcontextprotocol/sdk/server/mcp.js';
7
- import { z } from 'zod';
8
5
  import fs from 'node:fs';
9
6
  import { spawn } from 'node:child_process';
7
+ import { Buffer } from 'node:buffer';
8
+ import http from 'node:http';
9
+ import { StdioServerTransport } from '@modelcontextprotocol/sdk/server/stdio.js';
10
+ import { StreamableHTTPServerTransport } from '@modelcontextprotocol/sdk/server/streamableHttp.js';
11
+ import { McpServer, ResourceTemplate } from '@modelcontextprotocol/sdk/server/mcp.js';
12
+ import { z } from 'zod';
10
13
 
11
14
  const MCP_SERVER_NAME = "@weapp-vite/mcp";
12
15
  const MCP_SERVER_VERSION = "2.0.0";
@@ -643,11 +646,158 @@ async function createWeappViteMcpServer(options) {
643
646
  };
644
647
  }
645
648
 
646
- async function startStdioServer() {
647
- const { server } = await createWeappViteMcpServer();
648
- const transport = new StdioServerTransport();
649
- await server.connect(transport);
649
+ const DEFAULT_MCP_HOST = "127.0.0.1";
650
+ const DEFAULT_MCP_PORT = 3088;
651
+ const DEFAULT_MCP_ENDPOINT = "/mcp";
652
+ function normalizeEndpoint(input) {
653
+ const value = typeof input === "string" ? input.trim() : "";
654
+ if (!value) {
655
+ return DEFAULT_MCP_ENDPOINT;
656
+ }
657
+ return value.startsWith("/") ? value : `/${value}`;
658
+ }
659
+ function normalizePort(input) {
660
+ if (typeof input === "number" && Number.isInteger(input) && input > 0 && input <= 65535) {
661
+ return input;
662
+ }
663
+ return DEFAULT_MCP_PORT;
664
+ }
665
+ async function parseJsonBody(req) {
666
+ if (req.method !== "POST") {
667
+ return void 0;
668
+ }
669
+ const chunks = [];
670
+ for await (const chunk of req) {
671
+ chunks.push(typeof chunk === "string" ? Buffer.from(chunk) : chunk);
672
+ }
673
+ if (chunks.length === 0) {
674
+ return void 0;
675
+ }
676
+ const raw = Buffer.concat(chunks).toString("utf8").trim();
677
+ if (!raw) {
678
+ return void 0;
679
+ }
680
+ return JSON.parse(raw);
681
+ }
682
+ function writeJson(res, statusCode, payload) {
683
+ if (res.headersSent) {
684
+ return;
685
+ }
686
+ res.statusCode = statusCode;
687
+ res.setHeader("content-type", "application/json");
688
+ res.end(JSON.stringify(payload));
689
+ }
690
+ async function startStdioServer$1(options) {
691
+ const previousCwd = process.cwd();
692
+ if (options?.workspaceRoot) {
693
+ process.chdir(options.workspaceRoot);
694
+ }
695
+ try {
696
+ const { server } = await createWeappViteMcpServer(options);
697
+ const transport = new StdioServerTransport();
698
+ await server.connect(transport);
699
+ } finally {
700
+ if (options?.workspaceRoot) {
701
+ process.chdir(previousCwd);
702
+ }
703
+ }
704
+ }
705
+ async function startStreamableHttpServer(options) {
706
+ const {
707
+ endpoint = DEFAULT_MCP_ENDPOINT,
708
+ host = DEFAULT_MCP_HOST,
709
+ port = DEFAULT_MCP_PORT,
710
+ workspaceRoot,
711
+ unref = false,
712
+ quiet = false,
713
+ onReady
714
+ } = options;
715
+ const normalizedEndpoint = normalizeEndpoint(endpoint);
716
+ const normalizedPort = normalizePort(port);
717
+ const { server: mcpServer } = await createWeappViteMcpServer({ workspaceRoot });
718
+ const transport = new StreamableHTTPServerTransport({
719
+ sessionIdGenerator: void 0
720
+ });
721
+ await mcpServer.connect(transport);
722
+ const httpServer = http.createServer(async (req, res) => {
723
+ try {
724
+ const hostHeader = req.headers.host ?? `${host}:${normalizedPort}`;
725
+ const url = new URL(req.url ?? "/", `http://${hostHeader}`);
726
+ if (url.pathname !== normalizedEndpoint) {
727
+ writeJson(res, 404, {
728
+ jsonrpc: "2.0",
729
+ error: {
730
+ code: -32004,
731
+ message: `Not Found: ${url.pathname}`
732
+ },
733
+ id: null
734
+ });
735
+ return;
736
+ }
737
+ const method = req.method ?? "GET";
738
+ if (!["GET", "POST", "DELETE"].includes(method)) {
739
+ writeJson(res, 405, {
740
+ jsonrpc: "2.0",
741
+ error: {
742
+ code: -32005,
743
+ message: `Method Not Allowed: ${method}`
744
+ },
745
+ id: null
746
+ });
747
+ return;
748
+ }
749
+ const body = await parseJsonBody(req);
750
+ await transport.handleRequest(req, res, body);
751
+ } catch (error) {
752
+ writeJson(res, 500, {
753
+ jsonrpc: "2.0",
754
+ error: {
755
+ code: -32603,
756
+ message: error instanceof Error ? error.message : String(error)
757
+ },
758
+ id: null
759
+ });
760
+ }
761
+ });
762
+ await new Promise((resolve, reject) => {
763
+ httpServer.once("error", reject);
764
+ httpServer.listen(normalizedPort, host, () => {
765
+ resolve();
766
+ });
767
+ });
768
+ if (unref) {
769
+ httpServer.unref();
770
+ }
771
+ if (!quiet) {
772
+ onReady?.(`[mcp] streamable-http ready at http://${host}:${normalizedPort}${normalizedEndpoint}`);
773
+ }
774
+ return {
775
+ transport: "streamable-http",
776
+ close: async () => {
777
+ await transport.close();
778
+ await new Promise((resolve, reject) => {
779
+ httpServer.close((error) => {
780
+ if (error) {
781
+ reject(error);
782
+ return;
783
+ }
784
+ resolve();
785
+ });
786
+ });
787
+ }
788
+ };
789
+ }
790
+ async function startWeappViteMcpServer(options) {
791
+ const transport = options?.transport ?? "stdio";
792
+ if (transport === "streamable-http") {
793
+ return startStreamableHttpServer(options ?? {});
794
+ }
795
+ await startStdioServer$1(options);
796
+ return {
797
+ transport: "stdio"
798
+ };
650
799
  }
800
+
651
801
  function isDirectExecution() {
652
802
  const entry = process.argv[1];
653
803
  if (!entry) {
@@ -665,4 +815,4 @@ ${message}
665
815
  });
666
816
  }
667
817
 
668
- export { DEFAULT_MAX_FILE_CHARS, DEFAULT_MAX_OUTPUT_CHARS, DEFAULT_MAX_RESULTS, DEFAULT_TIMEOUT_MS, EXPOSED_PACKAGES, MCP_SERVER_NAME, MCP_SERVER_VERSION, SKIPPED_DIR_NAMES, assertInsideRoot, createWeappViteMcpServer, formatJson, listFilesInDirectory, loadExposedCatalog, loadPackageSummary, normalizeErrorMessage, readFileContent, resolveSubPath, resolveWorkspaceRoot, runCommand, searchTextInDirectory, startStdioServer, toToolError, toToolResult };
818
+ export { DEFAULT_MAX_FILE_CHARS, DEFAULT_MAX_OUTPUT_CHARS, DEFAULT_MAX_RESULTS, DEFAULT_MCP_ENDPOINT, DEFAULT_MCP_HOST, DEFAULT_MCP_PORT, DEFAULT_TIMEOUT_MS, EXPOSED_PACKAGES, MCP_SERVER_NAME, MCP_SERVER_VERSION, SKIPPED_DIR_NAMES, assertInsideRoot, createWeappViteMcpServer, formatJson, listFilesInDirectory, loadExposedCatalog, loadPackageSummary, normalizeErrorMessage, readFileContent, resolveSubPath, resolveWorkspaceRoot, runCommand, searchTextInDirectory, startStdioServer$1 as startStdioServer, startWeappViteMcpServer, toToolError, toToolResult };
package/package.json CHANGED
@@ -1,7 +1,7 @@
1
1
  {
2
2
  "name": "@weapp-vite/mcp",
3
3
  "type": "module",
4
- "version": "1.1.0",
4
+ "version": "1.1.1",
5
5
  "description": "mcp",
6
6
  "author": "ice breaker <1324318532@qq.com>",
7
7
  "license": "ISC",
@@ -40,6 +40,7 @@
40
40
  "build": "unbuild",
41
41
  "test": "vitest run",
42
42
  "test:dev": "vitest",
43
+ "typecheck": "tsc --noEmit",
43
44
  "release": "pnpm publish",
44
45
  "lint": "eslint .",
45
46
  "lint:fix": "eslint . --fix",