@smithery/sdk 1.5.10 → 1.6.2
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/dist/server/index.d.ts +1 -0
- package/dist/server/index.js +1 -0
- package/dist/server/oauth.d.ts +14 -0
- package/dist/server/oauth.js +36 -0
- package/dist/server/stateful.d.ts +7 -0
- package/dist/server/stateful.js +7 -0
- package/dist/server/stateless.d.ts +7 -0
- package/dist/server/stateless.js +7 -0
- package/package.json +1 -1
package/dist/server/index.d.ts
CHANGED
package/dist/server/index.js
CHANGED
|
@@ -0,0 +1,14 @@
|
|
|
1
|
+
import type express from "express";
|
|
2
|
+
import type { OAuthServerProvider } from "@modelcontextprotocol/sdk/server/auth/provider.js";
|
|
3
|
+
import type { Response } from "express";
|
|
4
|
+
/**
|
|
5
|
+
* OAuth server provider that supports a callback handler.
|
|
6
|
+
* The callback handler is invoked to catch the OAuth callback from the OAuth provider.
|
|
7
|
+
*/
|
|
8
|
+
export interface CallbackOAuthServerProvider extends OAuthServerProvider {
|
|
9
|
+
/** Provider-specific callback handler used by the SDK */
|
|
10
|
+
handleOAuthCallback?: (code: string, state: string | undefined, res: Response) => Promise<URL>;
|
|
11
|
+
basePath?: string;
|
|
12
|
+
callbackPath: string;
|
|
13
|
+
}
|
|
14
|
+
export declare function mountOAuth(app: express.Application, provider: CallbackOAuthServerProvider): void;
|
|
@@ -0,0 +1,36 @@
|
|
|
1
|
+
import { requireBearerAuth } from "@modelcontextprotocol/sdk/server/auth/middleware/bearerAuth.js";
|
|
2
|
+
import { mcpAuthRouter } from "@modelcontextprotocol/sdk/server/auth/router.js";
|
|
3
|
+
export function mountOAuth(app, provider) {
|
|
4
|
+
// Mount OAuth authorization and token routes with dynamic issuer URL and provider
|
|
5
|
+
app.use(provider.basePath ?? "/", (req, res, next) => {
|
|
6
|
+
const host = req.get("host") ?? "localhost";
|
|
7
|
+
// Issuer URL must be https
|
|
8
|
+
const issuerUrl = new URL(`https://${host}`);
|
|
9
|
+
const router = mcpAuthRouter({ provider, issuerUrl });
|
|
10
|
+
return router(req, res, next);
|
|
11
|
+
});
|
|
12
|
+
const callbackHandler = provider.handleOAuthCallback;
|
|
13
|
+
if (callbackHandler) {
|
|
14
|
+
// Callback handler
|
|
15
|
+
app.get(provider.callbackPath, async (req, res) => {
|
|
16
|
+
const code = typeof req.query.code === "string" ? req.query.code : undefined;
|
|
17
|
+
const state = typeof req.query.state === "string" ? req.query.state : undefined;
|
|
18
|
+
if (!code) {
|
|
19
|
+
res.status(400).send("Invalid request parameters");
|
|
20
|
+
return;
|
|
21
|
+
}
|
|
22
|
+
try {
|
|
23
|
+
const redirectUrl = await callbackHandler.bind(provider)(code, state, res);
|
|
24
|
+
res.redirect(redirectUrl.toString());
|
|
25
|
+
}
|
|
26
|
+
catch (error) {
|
|
27
|
+
console.error(error);
|
|
28
|
+
res.status(500).send("Error during authentication callback");
|
|
29
|
+
}
|
|
30
|
+
});
|
|
31
|
+
}
|
|
32
|
+
// Bearer protection for all /mcp routes (POST/GET/DELETE)
|
|
33
|
+
app.use("/mcp", (req, res, next) => {
|
|
34
|
+
return requireBearerAuth({ verifier: provider })(req, res, next);
|
|
35
|
+
});
|
|
36
|
+
}
|
|
@@ -1,14 +1,17 @@
|
|
|
1
1
|
import { StreamableHTTPServerTransport } from "@modelcontextprotocol/sdk/server/streamableHttp.js";
|
|
2
|
+
import type { AuthInfo } from "@modelcontextprotocol/sdk/server/auth/types.js";
|
|
2
3
|
import express from "express";
|
|
3
4
|
import type { z } from "zod";
|
|
4
5
|
import type { Server } from "@modelcontextprotocol/sdk/server/index.js";
|
|
5
6
|
import { type SessionStore } from "./session.js";
|
|
7
|
+
import type { CallbackOAuthServerProvider } from "./oauth.js";
|
|
6
8
|
/**
|
|
7
9
|
* Arguments when we create a new instance of your server
|
|
8
10
|
*/
|
|
9
11
|
export interface CreateServerArg<T = Record<string, unknown>> {
|
|
10
12
|
sessionId: string;
|
|
11
13
|
config: T;
|
|
14
|
+
auth?: AuthInfo;
|
|
12
15
|
}
|
|
13
16
|
export type CreateServerFn<T = Record<string, unknown>> = (arg: CreateServerArg<T>) => Server;
|
|
14
17
|
/**
|
|
@@ -27,6 +30,10 @@ export interface StatefulServerOptions<T = Record<string, unknown>> {
|
|
|
27
30
|
* Express app instance to use (optional)
|
|
28
31
|
*/
|
|
29
32
|
app?: express.Application;
|
|
33
|
+
/**
|
|
34
|
+
* OAuth provider instance. If provided, OAuth routes and bearer protection are auto-wired.
|
|
35
|
+
*/
|
|
36
|
+
oauthProvider?: CallbackOAuthServerProvider;
|
|
30
37
|
}
|
|
31
38
|
/**
|
|
32
39
|
* Creates a stateful server for handling MCP requests.
|
package/dist/server/stateful.js
CHANGED
|
@@ -5,6 +5,7 @@ import { randomUUID } from "node:crypto";
|
|
|
5
5
|
import { parseAndValidateConfig } from "../shared/config.js";
|
|
6
6
|
import { zodToJsonSchema } from "zod-to-json-schema";
|
|
7
7
|
import { createLRUStore } from "./session.js";
|
|
8
|
+
import { mountOAuth } from "./oauth.js";
|
|
8
9
|
/**
|
|
9
10
|
* Creates a stateful server for handling MCP requests.
|
|
10
11
|
* For every new session, we invoke createMcpServer to create a new instance of the server.
|
|
@@ -14,6 +15,11 @@ import { createLRUStore } from "./session.js";
|
|
|
14
15
|
*/
|
|
15
16
|
export function createStatefulServer(createMcpServer, options) {
|
|
16
17
|
const app = options?.app ?? express();
|
|
18
|
+
// Auto-wire OAuth routes and bearer protection if configured
|
|
19
|
+
const oauthProvider = options?.oauthProvider;
|
|
20
|
+
if (oauthProvider) {
|
|
21
|
+
mountOAuth(app, oauthProvider);
|
|
22
|
+
}
|
|
17
23
|
app.use("/mcp", express.json());
|
|
18
24
|
const sessionStore = options?.sessionStore ?? createLRUStore();
|
|
19
25
|
// Handle POST requests for client-to-server communication
|
|
@@ -54,6 +60,7 @@ export function createStatefulServer(createMcpServer, options) {
|
|
|
54
60
|
const server = createMcpServer({
|
|
55
61
|
sessionId: newSessionId,
|
|
56
62
|
config: config,
|
|
63
|
+
auth: req.auth,
|
|
57
64
|
});
|
|
58
65
|
// Connect to the MCP server
|
|
59
66
|
await server.connect(transport);
|
|
@@ -1,11 +1,14 @@
|
|
|
1
1
|
import express from "express";
|
|
2
2
|
import type { z } from "zod";
|
|
3
3
|
import type { Server } from "@modelcontextprotocol/sdk/server/index.js";
|
|
4
|
+
import type { AuthInfo } from "@modelcontextprotocol/sdk/server/auth/types.js";
|
|
5
|
+
import type { CallbackOAuthServerProvider } from "./oauth.js";
|
|
4
6
|
/**
|
|
5
7
|
* Arguments when we create a stateless server instance
|
|
6
8
|
*/
|
|
7
9
|
export interface CreateStatelessServerArg<T = Record<string, unknown>> {
|
|
8
10
|
config: T;
|
|
11
|
+
auth?: AuthInfo;
|
|
9
12
|
}
|
|
10
13
|
export type CreateStatelessServerFn<T = Record<string, unknown>> = (arg: CreateStatelessServerArg<T>) => Server;
|
|
11
14
|
/**
|
|
@@ -20,6 +23,10 @@ export interface StatelessServerOptions<T = Record<string, unknown>> {
|
|
|
20
23
|
* Express app instance to use (optional)
|
|
21
24
|
*/
|
|
22
25
|
app?: express.Application;
|
|
26
|
+
/**
|
|
27
|
+
* OAuth provider instance. If provided, OAuth routes and bearer protection are auto-wired.
|
|
28
|
+
*/
|
|
29
|
+
oauthProvider?: CallbackOAuthServerProvider;
|
|
23
30
|
}
|
|
24
31
|
/**
|
|
25
32
|
* Creates a stateless server for handling MCP requests.
|
package/dist/server/stateless.js
CHANGED
|
@@ -2,6 +2,7 @@ import { StreamableHTTPServerTransport } from "@modelcontextprotocol/sdk/server/
|
|
|
2
2
|
import express from "express";
|
|
3
3
|
import { parseAndValidateConfig } from "../shared/config.js";
|
|
4
4
|
import { zodToJsonSchema } from "zod-to-json-schema";
|
|
5
|
+
import { mountOAuth } from "./oauth.js";
|
|
5
6
|
/**
|
|
6
7
|
* Creates a stateless server for handling MCP requests.
|
|
7
8
|
* Each request creates a new server instance - no session state is maintained.
|
|
@@ -13,6 +14,11 @@ import { zodToJsonSchema } from "zod-to-json-schema";
|
|
|
13
14
|
*/
|
|
14
15
|
export function createStatelessServer(createMcpServer, options) {
|
|
15
16
|
const app = options?.app ?? express();
|
|
17
|
+
// Auto-wire OAuth routes and bearer protection if configured
|
|
18
|
+
const oauthProvider = options?.oauthProvider;
|
|
19
|
+
if (oauthProvider) {
|
|
20
|
+
mountOAuth(app, oauthProvider);
|
|
21
|
+
}
|
|
16
22
|
app.use("/mcp", express.json());
|
|
17
23
|
// Handle POST requests for client-to-server communication
|
|
18
24
|
app.post("/mcp", async (req, res) => {
|
|
@@ -31,6 +37,7 @@ export function createStatelessServer(createMcpServer, options) {
|
|
|
31
37
|
// Create a fresh server instance for each request
|
|
32
38
|
const server = createMcpServer({
|
|
33
39
|
config,
|
|
40
|
+
auth: req.auth,
|
|
34
41
|
});
|
|
35
42
|
// Create a new transport for this request (no session management)
|
|
36
43
|
const transport = new StreamableHTTPServerTransport({
|