@vitemcp/server 1.3.0 → 1.4.0
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 +59 -2
- package/dist/ViteMCP.cjs +125 -20
- package/dist/ViteMCP.cjs.map +1 -1
- package/dist/ViteMCP.d.cts +41 -3
- package/dist/ViteMCP.d.ts +41 -3
- package/dist/ViteMCP.js +114 -9
- package/dist/ViteMCP.js.map +1 -1
- package/package.json +1 -1
package/dist/ViteMCP.js
CHANGED
|
@@ -17,18 +17,23 @@ import {
|
|
|
17
17
|
// src/ViteMCP.ts
|
|
18
18
|
import {
|
|
19
19
|
acceptedContent,
|
|
20
|
+
bearerAuthChallengeResponse,
|
|
20
21
|
completable,
|
|
21
22
|
createMcpHandler,
|
|
22
23
|
fromJsonSchema,
|
|
24
|
+
getOAuthProtectedResourceMetadataUrl,
|
|
23
25
|
inputRequired,
|
|
24
26
|
localhostAllowedOrigins,
|
|
25
27
|
McpServer,
|
|
28
|
+
OAuthError,
|
|
29
|
+
OAuthErrorCode,
|
|
26
30
|
originValidationResponse,
|
|
27
31
|
ResourceTemplate as SDKResourceTemplate
|
|
28
32
|
} from "@modelcontextprotocol/server";
|
|
29
33
|
import {
|
|
30
34
|
serveStdio
|
|
31
35
|
} from "@modelcontextprotocol/server/stdio";
|
|
36
|
+
import { AsyncLocalStorage } from "async_hooks";
|
|
32
37
|
import { readFile } from "fs/promises";
|
|
33
38
|
import { Hono as Hono2 } from "hono";
|
|
34
39
|
import { cors } from "hono/cors";
|
|
@@ -472,6 +477,13 @@ var ViteMCP = class {
|
|
|
472
477
|
return this.#serverState;
|
|
473
478
|
}
|
|
474
479
|
#authenticate;
|
|
480
|
+
/**
|
|
481
|
+
* Carries the `authenticate` result from the HTTP route into the per-request
|
|
482
|
+
* server factory. The SDK invokes that factory from within `handler.fetch`,
|
|
483
|
+
* so the running store is always the one this request established — whereas
|
|
484
|
+
* a field on `this` would be shared by every concurrent request.
|
|
485
|
+
*/
|
|
486
|
+
#authStore = new AsyncLocalStorage();
|
|
475
487
|
#connectedServers = [];
|
|
476
488
|
#corsMiddleware = null;
|
|
477
489
|
#handler = null;
|
|
@@ -483,6 +495,13 @@ var ViteMCP = class {
|
|
|
483
495
|
#resources = [];
|
|
484
496
|
#resourceTemplates = [];
|
|
485
497
|
#serverState = "stopped" /* Stopped */;
|
|
498
|
+
/**
|
|
499
|
+
* Whether this server publishes an RFC 9728 protected-resource document, and
|
|
500
|
+
* so whether a 401 can point a client at it. Latched when the routes are
|
|
501
|
+
* mounted rather than read back from `auth` per rejection: the provider's
|
|
502
|
+
* config getter builds its proxy on demand and refuses once destroyed.
|
|
503
|
+
*/
|
|
504
|
+
#servesProtectedResourceMetadata = false;
|
|
486
505
|
#stdioHandle = null;
|
|
487
506
|
#tools = [];
|
|
488
507
|
constructor(options) {
|
|
@@ -665,6 +684,38 @@ var ViteMCP = class {
|
|
|
665
684
|
}
|
|
666
685
|
this.#serverState = "stopped" /* Stopped */;
|
|
667
686
|
}
|
|
687
|
+
/**
|
|
688
|
+
* Runs the `authenticate` hook and decides whether the exchange may proceed,
|
|
689
|
+
* returning either the store to run it under or the `Response` refusing it.
|
|
690
|
+
*
|
|
691
|
+
* Deliberately *not* done inside the per-request server factory: that
|
|
692
|
+
* factory's only return channel is an `McpServer`, so a refusal raised there
|
|
693
|
+
* is indistinguishable from a crash and reaches the client as a 500 with no
|
|
694
|
+
* `WWW-Authenticate` — the one header that tells an MCP client where to log
|
|
695
|
+
* in. Doing it here also covers the legacy fallback, which shares the same
|
|
696
|
+
* route, so no transport can be reached without passing this.
|
|
697
|
+
*/
|
|
698
|
+
async #authenticateRequest(request) {
|
|
699
|
+
if (!this.#authenticate) {
|
|
700
|
+
return { auth: void 0 };
|
|
701
|
+
}
|
|
702
|
+
let auth;
|
|
703
|
+
try {
|
|
704
|
+
auth = await this.#authenticate(request);
|
|
705
|
+
} catch (error) {
|
|
706
|
+
if (error instanceof Response) {
|
|
707
|
+
return error;
|
|
708
|
+
}
|
|
709
|
+
this.#logger.error("[ViteMCP error]", error);
|
|
710
|
+
return bearerAuthChallengeResponse(
|
|
711
|
+
new OAuthError(OAuthErrorCode.ServerError, "Internal Server Error")
|
|
712
|
+
);
|
|
713
|
+
}
|
|
714
|
+
if (!auth) {
|
|
715
|
+
return this.#options.allowAnonymous ? { auth: void 0 } : this.#unauthorized(request);
|
|
716
|
+
}
|
|
717
|
+
return { auth };
|
|
718
|
+
}
|
|
668
719
|
async #buildServer(auth) {
|
|
669
720
|
const server = new McpServer(
|
|
670
721
|
{ name: this.#options.name, version: this.#options.version },
|
|
@@ -697,7 +748,7 @@ var ViteMCP = class {
|
|
|
697
748
|
tool.timeoutMs,
|
|
698
749
|
tool.name
|
|
699
750
|
) : tool.execute(args, context));
|
|
700
|
-
return normalizeToolResult(result);
|
|
751
|
+
return normalizeToolResult(result, tool.outputSchema !== void 0);
|
|
701
752
|
} catch (error) {
|
|
702
753
|
if (error instanceof UserError) {
|
|
703
754
|
return {
|
|
@@ -877,11 +928,10 @@ var ViteMCP = class {
|
|
|
877
928
|
}
|
|
878
929
|
async #startHttp(config) {
|
|
879
930
|
this.#handler = createMcpHandler(
|
|
880
|
-
|
|
881
|
-
|
|
882
|
-
|
|
883
|
-
|
|
884
|
-
},
|
|
931
|
+
// Authentication has already run in the route below, which is the only
|
|
932
|
+
// place that can turn a refusal into an HTTP status. What reaches here
|
|
933
|
+
// is either an authenticated session or a deliberately anonymous one.
|
|
934
|
+
async () => this.#buildServer(this.#authStore.getStore()?.auth),
|
|
885
935
|
{
|
|
886
936
|
// Tradeoff: `server/discover` advertises only 2026-07-28, so legacy
|
|
887
937
|
// requests are answered unadvertised and skip Mcp-* validation.
|
|
@@ -903,6 +953,7 @@ var ViteMCP = class {
|
|
|
903
953
|
proxy: oauth.proxy
|
|
904
954
|
})
|
|
905
955
|
);
|
|
956
|
+
this.#servesProtectedResourceMetadata = Boolean(oauth.protectedResource);
|
|
906
957
|
}
|
|
907
958
|
const health = this.#options.health;
|
|
908
959
|
if (health?.enabled !== false) {
|
|
@@ -926,7 +977,11 @@ var ViteMCP = class {
|
|
|
926
977
|
if (rejected) {
|
|
927
978
|
return rejected;
|
|
928
979
|
}
|
|
929
|
-
|
|
980
|
+
const authenticated = await this.#authenticateRequest(c.req.raw);
|
|
981
|
+
if (authenticated instanceof Response) {
|
|
982
|
+
return authenticated;
|
|
983
|
+
}
|
|
984
|
+
return this.#authStore.run(authenticated, () => handler.fetch(c.req.raw));
|
|
930
985
|
});
|
|
931
986
|
const app = this.#honoApp;
|
|
932
987
|
const maxBodySize = config.maxBodySize ?? OAUTH_PROXY_MAX_BODY_SIZE;
|
|
@@ -1017,6 +1072,26 @@ var ViteMCP = class {
|
|
|
1017
1072
|
);
|
|
1018
1073
|
}
|
|
1019
1074
|
}
|
|
1075
|
+
/**
|
|
1076
|
+
* The RFC 6750 §3 challenge sent when `authenticate` refuses a request.
|
|
1077
|
+
*
|
|
1078
|
+
* `resource_metadata` (RFC 9728 §5.1) is the parameter that turns a bare
|
|
1079
|
+
* rejection into a login: it points the client at this server's
|
|
1080
|
+
* protected-resource document, from which it discovers the authorization
|
|
1081
|
+
* server and starts the flow. It is derived from the URL the request
|
|
1082
|
+
* arrived on, which is exactly where `createOAuthRouter` mounts that
|
|
1083
|
+
* document, and is omitted when this server publishes no such document.
|
|
1084
|
+
*/
|
|
1085
|
+
#unauthorized(request) {
|
|
1086
|
+
return bearerAuthChallengeResponse(
|
|
1087
|
+
new OAuthError(OAuthErrorCode.InvalidToken, "Authentication required"),
|
|
1088
|
+
this.#servesProtectedResourceMetadata ? {
|
|
1089
|
+
resourceMetadataUrl: getOAuthProtectedResourceMetadataUrl(
|
|
1090
|
+
new URL(request.url)
|
|
1091
|
+
)
|
|
1092
|
+
} : {}
|
|
1093
|
+
);
|
|
1094
|
+
}
|
|
1020
1095
|
};
|
|
1021
1096
|
var nodeToWebRequest = (req, body, secure) => {
|
|
1022
1097
|
const host = req.headers.host ?? "localhost";
|
|
@@ -1110,7 +1185,21 @@ var promptArgsSchema = (prompt) => {
|
|
|
1110
1185
|
}
|
|
1111
1186
|
return z.object(shape);
|
|
1112
1187
|
};
|
|
1113
|
-
var
|
|
1188
|
+
var isStructuredResult = (value) => {
|
|
1189
|
+
if (!Object.hasOwn(value, "structuredContent") || value.structuredContent === void 0) {
|
|
1190
|
+
return false;
|
|
1191
|
+
}
|
|
1192
|
+
if (value.isError !== void 0 && typeof value.isError !== "boolean") {
|
|
1193
|
+
return false;
|
|
1194
|
+
}
|
|
1195
|
+
if (value._meta !== void 0 && (typeof value._meta !== "object" || value._meta === null || Array.isArray(value._meta))) {
|
|
1196
|
+
return false;
|
|
1197
|
+
}
|
|
1198
|
+
return Object.keys(value).every(
|
|
1199
|
+
(key) => key === "structuredContent" || key === "isError" || key === "_meta" || value[key] === void 0
|
|
1200
|
+
);
|
|
1201
|
+
};
|
|
1202
|
+
var normalizeToolResult = (result, hasOutputSchema) => {
|
|
1114
1203
|
if (result === void 0 || result === null) {
|
|
1115
1204
|
return { content: [] };
|
|
1116
1205
|
}
|
|
@@ -1122,7 +1211,23 @@ var normalizeToolResult = (result) => {
|
|
|
1122
1211
|
if (value.resultType === "input_required" || Array.isArray(value.content)) {
|
|
1123
1212
|
return value;
|
|
1124
1213
|
}
|
|
1125
|
-
if (value
|
|
1214
|
+
if (isStructuredResult(value)) {
|
|
1215
|
+
return {
|
|
1216
|
+
content: [
|
|
1217
|
+
{
|
|
1218
|
+
// `undefined` is excluded already; this covers what else JSON
|
|
1219
|
+
// declines to represent, so a broken payload stays a tool error
|
|
1220
|
+
// rather than an unserializable content block.
|
|
1221
|
+
text: JSON.stringify(value.structuredContent) ?? "null",
|
|
1222
|
+
type: "text"
|
|
1223
|
+
}
|
|
1224
|
+
],
|
|
1225
|
+
structuredContent: value.structuredContent,
|
|
1226
|
+
...value.isError === void 0 ? {} : { isError: value.isError },
|
|
1227
|
+
...value._meta === void 0 ? {} : { _meta: value._meta }
|
|
1228
|
+
};
|
|
1229
|
+
}
|
|
1230
|
+
if (!hasOutputSchema && (value.type === "text" || value.type === "image" || value.type === "audio" || value.type === "resource" || value.type === "resource_link")) {
|
|
1126
1231
|
return { content: [value] };
|
|
1127
1232
|
}
|
|
1128
1233
|
return {
|