fastmcp 3.20.0 → 3.20.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/package.json +5 -2
- package/.github/workflows/feature.yaml +0 -39
- package/.github/workflows/main.yaml +0 -50
- package/.prettierignore +0 -1
- package/.roo/mcp.json +0 -11
- package/eslint.config.ts +0 -14
- package/jsr.json +0 -7
- package/src/FastMCP.oauth.test.ts +0 -225
- package/src/FastMCP.session-context.test.ts +0 -136
- package/src/FastMCP.session-id.test.ts +0 -359
- package/src/FastMCP.test.ts +0 -4389
- package/src/FastMCP.ts +0 -2548
- package/src/bin/fastmcp.ts +0 -191
- package/src/examples/addition.ts +0 -333
- package/src/examples/custom-logger.ts +0 -201
- package/src/examples/oauth-server.ts +0 -113
- package/src/examples/session-context.ts +0 -269
- package/src/examples/session-id-counter.ts +0 -230
- package/tsconfig.json +0 -8
- package/vitest.config.js +0 -9
|
@@ -1,201 +0,0 @@
|
|
|
1
|
-
/**
|
|
2
|
-
* Example FastMCP server demonstrating custom logger implementations.
|
|
3
|
-
*
|
|
4
|
-
* Features demonstrated:
|
|
5
|
-
* - Simple custom logger implementation
|
|
6
|
-
* - File-based logging example
|
|
7
|
-
* - Winston logger adapter
|
|
8
|
-
* - Pino logger adapter
|
|
9
|
-
*
|
|
10
|
-
*/
|
|
11
|
-
|
|
12
|
-
import { z } from "zod";
|
|
13
|
-
|
|
14
|
-
import { FastMCP, Logger } from "../FastMCP.js";
|
|
15
|
-
|
|
16
|
-
// Example 1: Simple Custom Logger Implementation
|
|
17
|
-
class SimpleCustomLogger implements Logger {
|
|
18
|
-
debug(...args: unknown[]): void {
|
|
19
|
-
console.log("[CUSTOM DEBUG]", new Date().toISOString(), ...args);
|
|
20
|
-
}
|
|
21
|
-
|
|
22
|
-
error(...args: unknown[]): void {
|
|
23
|
-
console.error("[CUSTOM ERROR]", new Date().toISOString(), ...args);
|
|
24
|
-
}
|
|
25
|
-
|
|
26
|
-
info(...args: unknown[]): void {
|
|
27
|
-
console.info("[CUSTOM INFO]", new Date().toISOString(), ...args);
|
|
28
|
-
}
|
|
29
|
-
|
|
30
|
-
log(...args: unknown[]): void {
|
|
31
|
-
console.log("[CUSTOM LOG]", new Date().toISOString(), ...args);
|
|
32
|
-
}
|
|
33
|
-
|
|
34
|
-
warn(...args: unknown[]): void {
|
|
35
|
-
console.warn("[CUSTOM WARN]", new Date().toISOString(), ...args);
|
|
36
|
-
}
|
|
37
|
-
}
|
|
38
|
-
|
|
39
|
-
// Example 2: File-based Logger
|
|
40
|
-
// class FileLogger implements Logger {
|
|
41
|
-
// debug(...args: unknown[]): void {
|
|
42
|
-
// this.logToFile('DEBUG', ...args);
|
|
43
|
-
// }
|
|
44
|
-
|
|
45
|
-
// error(...args: unknown[]): void {
|
|
46
|
-
// this.logToFile('ERROR', ...args);
|
|
47
|
-
// }
|
|
48
|
-
|
|
49
|
-
// info(...args: unknown[]): void {
|
|
50
|
-
// this.logToFile('INFO', ...args);
|
|
51
|
-
// }
|
|
52
|
-
|
|
53
|
-
// log(...args: unknown[]): void {
|
|
54
|
-
// this.logToFile('LOG', ...args);
|
|
55
|
-
// }
|
|
56
|
-
|
|
57
|
-
// warn(...args: unknown[]): void {
|
|
58
|
-
// this.logToFile('WARN', ...args);
|
|
59
|
-
// }
|
|
60
|
-
|
|
61
|
-
// private logToFile(level: string, ...args: unknown[]): void {
|
|
62
|
-
// const timestamp = new Date().toISOString();
|
|
63
|
-
// const message = `[${timestamp}] [${level}] ${args.map(arg =>
|
|
64
|
-
// typeof arg === 'object' ? JSON.stringify(arg) : String(arg)
|
|
65
|
-
// ).join(' ')}\n`;
|
|
66
|
-
|
|
67
|
-
// // In a real implementation, you might use fs.appendFile or a logging library
|
|
68
|
-
// console.log(message.trim());
|
|
69
|
-
// }
|
|
70
|
-
// }
|
|
71
|
-
|
|
72
|
-
// Example 3: Winston Logger Adapter
|
|
73
|
-
// To use this example, install winston: npm install winston
|
|
74
|
-
// import winston from 'winston';
|
|
75
|
-
|
|
76
|
-
// class WinstonLoggerAdapter implements Logger {
|
|
77
|
-
// private winston: winston.Logger;
|
|
78
|
-
|
|
79
|
-
// constructor() {
|
|
80
|
-
// this.winston = winston.createLogger({
|
|
81
|
-
// level: 'debug',
|
|
82
|
-
// format: winston.format.combine(
|
|
83
|
-
// winston.format.timestamp(),
|
|
84
|
-
// winston.format.errors({ stack: true }),
|
|
85
|
-
// winston.format.json()
|
|
86
|
-
// ),
|
|
87
|
-
// transports: [
|
|
88
|
-
// new winston.transports.Console({
|
|
89
|
-
// format: winston.format.combine(
|
|
90
|
-
// winston.format.colorize(),
|
|
91
|
-
// winston.format.simple()
|
|
92
|
-
// )
|
|
93
|
-
// }),
|
|
94
|
-
// new winston.transports.File({ filename: 'fastmcp.log' })
|
|
95
|
-
// ]
|
|
96
|
-
// });
|
|
97
|
-
// }
|
|
98
|
-
|
|
99
|
-
// debug(...args: unknown[]): void {
|
|
100
|
-
// this.winston.debug(this.formatArgs(args));
|
|
101
|
-
// }
|
|
102
|
-
|
|
103
|
-
// error(...args: unknown[]): void {
|
|
104
|
-
// this.winston.error(this.formatArgs(args));
|
|
105
|
-
// }
|
|
106
|
-
|
|
107
|
-
// info(...args: unknown[]): void {
|
|
108
|
-
// this.winston.info(this.formatArgs(args));
|
|
109
|
-
// }
|
|
110
|
-
|
|
111
|
-
// log(...args: unknown[]): void {
|
|
112
|
-
// this.winston.info(this.formatArgs(args));
|
|
113
|
-
// }
|
|
114
|
-
|
|
115
|
-
// warn(...args: unknown[]): void {
|
|
116
|
-
// this.winston.warn(this.formatArgs(args));
|
|
117
|
-
// }
|
|
118
|
-
|
|
119
|
-
// private formatArgs(args: unknown[]): string {
|
|
120
|
-
// return args.map(arg =>
|
|
121
|
-
// typeof arg === 'object' ? JSON.stringify(arg) : String(arg)
|
|
122
|
-
// ).join(' ');
|
|
123
|
-
// }
|
|
124
|
-
// }
|
|
125
|
-
|
|
126
|
-
// Example 4: Pino Logger Adapter
|
|
127
|
-
// To use this example, install pino: npm install pino
|
|
128
|
-
// import pino from 'pino';
|
|
129
|
-
//
|
|
130
|
-
// class PinoLoggerAdapter implements Logger {
|
|
131
|
-
// private pino: pino.Logger;
|
|
132
|
-
//
|
|
133
|
-
// constructor() {
|
|
134
|
-
// this.pino = pino({
|
|
135
|
-
// level: 'debug',
|
|
136
|
-
// transport: {
|
|
137
|
-
// target: 'pino-pretty',
|
|
138
|
-
// options: {
|
|
139
|
-
// colorize: true,
|
|
140
|
-
// translateTime: 'SYS:standard',
|
|
141
|
-
// ignore: 'pid,hostname'
|
|
142
|
-
// }
|
|
143
|
-
// }
|
|
144
|
-
// });
|
|
145
|
-
// }
|
|
146
|
-
//
|
|
147
|
-
// debug(...args: unknown[]): void {
|
|
148
|
-
// this.pino.debug(this.formatMessage(args));
|
|
149
|
-
// }
|
|
150
|
-
//
|
|
151
|
-
// error(...args: unknown[]): void {
|
|
152
|
-
// this.pino.error(this.formatMessage(args));
|
|
153
|
-
// }
|
|
154
|
-
//
|
|
155
|
-
// info(...args: unknown[]): void {
|
|
156
|
-
// this.pino.info(this.formatMessage(args));
|
|
157
|
-
// }
|
|
158
|
-
//
|
|
159
|
-
// log(...args: unknown[]): void {
|
|
160
|
-
// this.pino.info(this.formatMessage(args));
|
|
161
|
-
// }
|
|
162
|
-
//
|
|
163
|
-
// warn(...args: unknown[]): void {
|
|
164
|
-
// this.pino.warn(this.formatMessage(args));
|
|
165
|
-
// }
|
|
166
|
-
//
|
|
167
|
-
// private formatMessage(args: unknown[]): string {
|
|
168
|
-
// return args.map(arg =>
|
|
169
|
-
// typeof arg === 'object' ? JSON.stringify(arg) : String(arg)
|
|
170
|
-
// ).join(' ');
|
|
171
|
-
// }
|
|
172
|
-
// }
|
|
173
|
-
|
|
174
|
-
// Choose which logger to use (uncomment the one you want to use)
|
|
175
|
-
const logger = new SimpleCustomLogger();
|
|
176
|
-
// const logger = new FileLogger();
|
|
177
|
-
// const logger = new WinstonLoggerAdapter();
|
|
178
|
-
// const logger = new PinoLoggerAdapter();
|
|
179
|
-
|
|
180
|
-
const server = new FastMCP({
|
|
181
|
-
logger: logger,
|
|
182
|
-
name: "custom-logger-example",
|
|
183
|
-
version: "1.0.0",
|
|
184
|
-
});
|
|
185
|
-
|
|
186
|
-
server.addTool({
|
|
187
|
-
description: "A test tool that demonstrates custom logging",
|
|
188
|
-
execute: async (args) => {
|
|
189
|
-
return `Received: ${args.message}`;
|
|
190
|
-
},
|
|
191
|
-
name: "test_tool",
|
|
192
|
-
parameters: z.object({
|
|
193
|
-
message: z.string().describe("A message to log"),
|
|
194
|
-
}),
|
|
195
|
-
});
|
|
196
|
-
|
|
197
|
-
// Start the server with stdio transport
|
|
198
|
-
server.start({ transportType: "stdio" }).catch((error: unknown) => {
|
|
199
|
-
console.error("Failed to start server:", error);
|
|
200
|
-
process.exit(1);
|
|
201
|
-
});
|
|
@@ -1,113 +0,0 @@
|
|
|
1
|
-
/**
|
|
2
|
-
* Example FastMCP server demonstrating OAuth well-known endpoint support.
|
|
3
|
-
*
|
|
4
|
-
* This example shows how to configure FastMCP to serve OAuth discovery endpoints
|
|
5
|
-
* for both authorization server metadata and protected resource metadata.
|
|
6
|
-
*
|
|
7
|
-
* Run with: node dist/examples/oauth-server.js --transport http-stream --port 4111
|
|
8
|
-
* Then visit:
|
|
9
|
-
* - http://localhost:4111/.well-known/oauth-authorization-server
|
|
10
|
-
* - http://localhost:4111/.well-known/oauth-protected-resource
|
|
11
|
-
*/
|
|
12
|
-
|
|
13
|
-
import { FastMCP } from "../FastMCP.js";
|
|
14
|
-
|
|
15
|
-
const server = new FastMCP({
|
|
16
|
-
name: "OAuth Example Server",
|
|
17
|
-
oauth: {
|
|
18
|
-
authorizationServer: {
|
|
19
|
-
authorizationEndpoint: "https://auth.example.com/oauth/authorize",
|
|
20
|
-
codeChallengeMethodsSupported: ["S256"],
|
|
21
|
-
// DPoP support
|
|
22
|
-
dpopSigningAlgValuesSupported: ["ES256", "RS256"],
|
|
23
|
-
grantTypesSupported: ["authorization_code", "refresh_token"],
|
|
24
|
-
|
|
25
|
-
introspectionEndpoint: "https://auth.example.com/oauth/introspect",
|
|
26
|
-
// Required fields
|
|
27
|
-
issuer: "https://auth.example.com",
|
|
28
|
-
// Optional fields
|
|
29
|
-
jwksUri: "https://auth.example.com/.well-known/jwks.json",
|
|
30
|
-
opPolicyUri: "https://example.com/policy",
|
|
31
|
-
opTosUri: "https://example.com/terms",
|
|
32
|
-
registrationEndpoint: "https://auth.example.com/oauth/register",
|
|
33
|
-
responseModesSupported: ["query", "fragment"],
|
|
34
|
-
responseTypesSupported: ["code"],
|
|
35
|
-
revocationEndpoint: "https://auth.example.com/oauth/revoke",
|
|
36
|
-
scopesSupported: ["read", "write", "admin"],
|
|
37
|
-
serviceDocumentation: "https://docs.example.com/oauth",
|
|
38
|
-
tokenEndpoint: "https://auth.example.com/oauth/token",
|
|
39
|
-
tokenEndpointAuthMethodsSupported: [
|
|
40
|
-
"client_secret_basic",
|
|
41
|
-
"client_secret_post",
|
|
42
|
-
],
|
|
43
|
-
tokenEndpointAuthSigningAlgValuesSupported: ["RS256", "ES256"],
|
|
44
|
-
|
|
45
|
-
uiLocalesSupported: ["en-US", "es-ES"],
|
|
46
|
-
},
|
|
47
|
-
enabled: true,
|
|
48
|
-
protectedResource: {
|
|
49
|
-
authorizationDetailsTypesSupported: [
|
|
50
|
-
"payment_initiation",
|
|
51
|
-
"account_access",
|
|
52
|
-
],
|
|
53
|
-
authorizationServers: ["https://auth.example.com"],
|
|
54
|
-
bearerMethodsSupported: ["header"],
|
|
55
|
-
dpopBoundAccessTokensRequired: false,
|
|
56
|
-
dpopSigningAlgValuesSupported: ["ES256", "RS256"],
|
|
57
|
-
jwksUri: "https://oauth-example-server.example.com/.well-known/jwks.json",
|
|
58
|
-
resource: "mcp://oauth-example-server",
|
|
59
|
-
resourceDocumentation: "https://docs.example.com/mcp-api",
|
|
60
|
-
resourceName: "OAuth Example API",
|
|
61
|
-
resourcePolicyUri: "https://example.com/resource-policy",
|
|
62
|
-
resourceSigningAlgValuesSupported: ["RS256", "ES256"],
|
|
63
|
-
resourceTosUri: "https://example.com/terms-of-service",
|
|
64
|
-
scopesSupported: ["read", "write", "admin"],
|
|
65
|
-
serviceDocumentation: "https://developer.example.com/api-docs",
|
|
66
|
-
tlsClientCertificateBoundAccessTokens: false,
|
|
67
|
-
},
|
|
68
|
-
},
|
|
69
|
-
version: "1.0.0",
|
|
70
|
-
});
|
|
71
|
-
|
|
72
|
-
// Add a simple tool to demonstrate the server functionality
|
|
73
|
-
server.addTool({
|
|
74
|
-
description: "Get information about this OAuth-enabled MCP server",
|
|
75
|
-
execute: async () => {
|
|
76
|
-
return {
|
|
77
|
-
content: [
|
|
78
|
-
{
|
|
79
|
-
text: `This is an OAuth-enabled FastMCP server!
|
|
80
|
-
|
|
81
|
-
OAuth Discovery Endpoints:
|
|
82
|
-
- Authorization Server: /.well-known/oauth-authorization-server
|
|
83
|
-
- Protected Resource: /.well-known/oauth-protected-resource
|
|
84
|
-
|
|
85
|
-
The server demonstrates how to configure OAuth metadata for MCP servers
|
|
86
|
-
that need to integrate with OAuth 2.0 authorization flows.`,
|
|
87
|
-
type: "text",
|
|
88
|
-
},
|
|
89
|
-
],
|
|
90
|
-
};
|
|
91
|
-
},
|
|
92
|
-
name: "get-server-info",
|
|
93
|
-
});
|
|
94
|
-
|
|
95
|
-
// Start the server
|
|
96
|
-
await server.start({
|
|
97
|
-
httpStream: { port: 4111 },
|
|
98
|
-
transportType: "httpStream",
|
|
99
|
-
});
|
|
100
|
-
|
|
101
|
-
console.log(`
|
|
102
|
-
🚀 OAuth Example Server is running!
|
|
103
|
-
|
|
104
|
-
Try these endpoints:
|
|
105
|
-
- MCP (HTTP Stream): http://localhost:4111/mcp
|
|
106
|
-
- MCP (SSE): http://localhost:4111/sse
|
|
107
|
-
- Health: http://localhost:4111/health
|
|
108
|
-
- OAuth Authorization Server: http://localhost:4111/.well-known/oauth-authorization-server
|
|
109
|
-
- OAuth Protected Resource: http://localhost:4111/.well-known/oauth-protected-resource
|
|
110
|
-
|
|
111
|
-
The OAuth endpoints work with both SSE and HTTP Stream transports and return
|
|
112
|
-
JSON metadata following RFC 8414 standards.
|
|
113
|
-
`);
|
|
@@ -1,269 +0,0 @@
|
|
|
1
|
-
/**
|
|
2
|
-
* Example demonstrating session context support in FastMCP stdio transport
|
|
3
|
-
*
|
|
4
|
-
* This example demonstrates the fix for issue #144:
|
|
5
|
-
* Session context is now properly passed to tool execution handlers
|
|
6
|
-
* when using stdio transport with an authenticate function.
|
|
7
|
-
*
|
|
8
|
-
* To run this example:
|
|
9
|
-
* npx fastmcp dev src/examples/session-context.ts
|
|
10
|
-
*/
|
|
11
|
-
|
|
12
|
-
import { z } from "zod";
|
|
13
|
-
|
|
14
|
-
import { FastMCP } from "../FastMCP.js";
|
|
15
|
-
|
|
16
|
-
interface UserSession {
|
|
17
|
-
[key: string]: unknown;
|
|
18
|
-
permissions: string[];
|
|
19
|
-
role: "admin" | "guest" | "user";
|
|
20
|
-
userId: string;
|
|
21
|
-
username: string;
|
|
22
|
-
}
|
|
23
|
-
|
|
24
|
-
const server = new FastMCP<UserSession>({
|
|
25
|
-
authenticate: async (request) => {
|
|
26
|
-
if (!request) {
|
|
27
|
-
console.log(
|
|
28
|
-
"[Auth] Authenticating stdio transport using environment variables",
|
|
29
|
-
);
|
|
30
|
-
|
|
31
|
-
const userId = process.env.USER_ID || "default-user";
|
|
32
|
-
const username = process.env.USERNAME || "Anonymous";
|
|
33
|
-
const role =
|
|
34
|
-
(process.env.USER_ROLE as "admin" | "guest" | "user") || "guest";
|
|
35
|
-
// Mock permissions based on role
|
|
36
|
-
const permissions =
|
|
37
|
-
role === "admin"
|
|
38
|
-
? ["read", "write", "delete", "admin"]
|
|
39
|
-
: role === "user"
|
|
40
|
-
? ["read", "write"]
|
|
41
|
-
: ["read"];
|
|
42
|
-
const session: UserSession = {
|
|
43
|
-
authenticatedAt: new Date().toISOString(),
|
|
44
|
-
permissions,
|
|
45
|
-
role,
|
|
46
|
-
userId,
|
|
47
|
-
username,
|
|
48
|
-
};
|
|
49
|
-
|
|
50
|
-
console.log(`[Auth] Authenticated user: ${username} (${role})`);
|
|
51
|
-
|
|
52
|
-
return session;
|
|
53
|
-
}
|
|
54
|
-
|
|
55
|
-
// For HTTP transport (request contains headers)
|
|
56
|
-
console.log("[Auth] Authenticating HTTP transport using headers");
|
|
57
|
-
|
|
58
|
-
const authHeader = request.headers["authorization"] as string;
|
|
59
|
-
|
|
60
|
-
if (!authHeader || !authHeader.startsWith("Bearer ")) {
|
|
61
|
-
throw new Response("Missing or invalid authorization header", {
|
|
62
|
-
status: 401,
|
|
63
|
-
});
|
|
64
|
-
}
|
|
65
|
-
|
|
66
|
-
const token = authHeader.substring(7);
|
|
67
|
-
|
|
68
|
-
// Mock token validation (in real implementation, validate against your auth service)
|
|
69
|
-
if (token === "admin-token") {
|
|
70
|
-
return {
|
|
71
|
-
authenticatedAt: new Date().toISOString(),
|
|
72
|
-
permissions: ["read", "write", "delete", "admin"],
|
|
73
|
-
role: "admin" as const,
|
|
74
|
-
userId: "admin-001",
|
|
75
|
-
username: "Administrator",
|
|
76
|
-
};
|
|
77
|
-
} else if (token === "user-token") {
|
|
78
|
-
return {
|
|
79
|
-
authenticatedAt: new Date().toISOString(),
|
|
80
|
-
permissions: ["read", "write"],
|
|
81
|
-
role: "user" as const,
|
|
82
|
-
userId: "user-001",
|
|
83
|
-
username: "Regular User",
|
|
84
|
-
};
|
|
85
|
-
}
|
|
86
|
-
|
|
87
|
-
throw new Response("Invalid token", { status: 401 });
|
|
88
|
-
},
|
|
89
|
-
name: "Session Context Demo",
|
|
90
|
-
version: "1.0.0",
|
|
91
|
-
});
|
|
92
|
-
|
|
93
|
-
// Tool that demonstrates session context access
|
|
94
|
-
server.addTool({
|
|
95
|
-
description: "Get information about the current authenticated user",
|
|
96
|
-
execute: async (_args, context) => {
|
|
97
|
-
if (!context.session)
|
|
98
|
-
return "No session context available (this shouldn't happen after the fix!)";
|
|
99
|
-
|
|
100
|
-
const { session } = context;
|
|
101
|
-
|
|
102
|
-
return `✓ Session Context Available!
|
|
103
|
-
|
|
104
|
-
User Info:
|
|
105
|
-
- User ID: ${session.userId}
|
|
106
|
-
- Username: ${session.username}
|
|
107
|
-
- Role: ${session.role}
|
|
108
|
-
- Permissions: ${session.permissions.join(", ")}
|
|
109
|
-
- Authenticated At: ${session.authenticatedAt}`;
|
|
110
|
-
},
|
|
111
|
-
name: "whoami",
|
|
112
|
-
});
|
|
113
|
-
|
|
114
|
-
// Tool that demonstrates role-based access
|
|
115
|
-
server.addTool({
|
|
116
|
-
description: "Perform an admin-only operation (requires admin role)",
|
|
117
|
-
execute: async (args, context) => {
|
|
118
|
-
if (!context.session)
|
|
119
|
-
return "No session context - cannot verify permissions";
|
|
120
|
-
if (context.session.role !== "admin")
|
|
121
|
-
return `Access denied. Current role: ${context.session.role}, required: admin`;
|
|
122
|
-
if (!context.session.permissions.includes("admin"))
|
|
123
|
-
return "Insufficient permissions for admin operations";
|
|
124
|
-
|
|
125
|
-
return `✓ Admin operation "${args.action}" executed successfully by ${context.session.username}`;
|
|
126
|
-
},
|
|
127
|
-
name: "admin-operation",
|
|
128
|
-
parameters: z.object({
|
|
129
|
-
action: z.string().describe("The admin action to perform"),
|
|
130
|
-
}),
|
|
131
|
-
});
|
|
132
|
-
|
|
133
|
-
// Tool that demonstrates permission checks
|
|
134
|
-
server.addTool({
|
|
135
|
-
description: "Check what permissions the current user has",
|
|
136
|
-
execute: async (args, context) => {
|
|
137
|
-
if (!context.session) return "No session context available";
|
|
138
|
-
|
|
139
|
-
const hasPermission = context.session.permissions.includes(args.operation);
|
|
140
|
-
|
|
141
|
-
return `Permission Check for "${args.operation}":
|
|
142
|
-
${hasPermission ? "✓ ALLOWED" : "! DENIED"}
|
|
143
|
-
|
|
144
|
-
Your permissions: ${context.session.permissions.join(", ")}
|
|
145
|
-
Your role: ${context.session.role}`;
|
|
146
|
-
},
|
|
147
|
-
name: "check-permissions",
|
|
148
|
-
parameters: z.object({
|
|
149
|
-
operation: z.string().describe("Operation to check permission for"),
|
|
150
|
-
}),
|
|
151
|
-
});
|
|
152
|
-
|
|
153
|
-
// Resource that uses session context
|
|
154
|
-
server.addResource({
|
|
155
|
-
description: "Get detailed information about the current authenticated user",
|
|
156
|
-
load: async (auth) => {
|
|
157
|
-
if (!auth) {
|
|
158
|
-
return {
|
|
159
|
-
text: JSON.stringify(
|
|
160
|
-
{
|
|
161
|
-
authenticated: false,
|
|
162
|
-
error: "No authentication context available",
|
|
163
|
-
},
|
|
164
|
-
|
|
165
|
-
null,
|
|
166
|
-
2,
|
|
167
|
-
),
|
|
168
|
-
};
|
|
169
|
-
}
|
|
170
|
-
|
|
171
|
-
return {
|
|
172
|
-
text: JSON.stringify(
|
|
173
|
-
{
|
|
174
|
-
authenticated: true,
|
|
175
|
-
user: {
|
|
176
|
-
authenticatedAt: auth.authenticatedAt,
|
|
177
|
-
id: auth.userId,
|
|
178
|
-
permissions: auth.permissions,
|
|
179
|
-
role: auth.role,
|
|
180
|
-
username: auth.username,
|
|
181
|
-
},
|
|
182
|
-
},
|
|
183
|
-
|
|
184
|
-
null,
|
|
185
|
-
2,
|
|
186
|
-
),
|
|
187
|
-
};
|
|
188
|
-
},
|
|
189
|
-
mimeType: "application/json",
|
|
190
|
-
name: "Current User Information",
|
|
191
|
-
uri: "session://current-user",
|
|
192
|
-
});
|
|
193
|
-
|
|
194
|
-
// Prompt that uses session context
|
|
195
|
-
server.addPrompt({
|
|
196
|
-
arguments: [
|
|
197
|
-
{
|
|
198
|
-
description: "Greeting style (formal, casual, friendly)",
|
|
199
|
-
name: "style",
|
|
200
|
-
required: false,
|
|
201
|
-
},
|
|
202
|
-
],
|
|
203
|
-
description: "Generate a personalized greeting based on the current user",
|
|
204
|
-
load: async (args, auth) => {
|
|
205
|
-
const style = args.style || "friendly";
|
|
206
|
-
|
|
207
|
-
if (!auth) {
|
|
208
|
-
return "Hello! I don't have access to your session information.";
|
|
209
|
-
}
|
|
210
|
-
|
|
211
|
-
const greetings = {
|
|
212
|
-
casual: `Hey ${auth.username}! Nice to see you again.`,
|
|
213
|
-
formal: `Good day, ${auth.username}. You are logged in with ${auth.role} privileges.`,
|
|
214
|
-
friendly: `Hello ${auth.username}! 😊 You're logged in as a ${auth.role}. How can I help you today?`,
|
|
215
|
-
};
|
|
216
|
-
|
|
217
|
-
return greetings[style as keyof typeof greetings] || greetings.friendly;
|
|
218
|
-
},
|
|
219
|
-
name: "personalized-greeting",
|
|
220
|
-
});
|
|
221
|
-
|
|
222
|
-
// Start the server
|
|
223
|
-
if (process.argv.includes("--http-stream")) {
|
|
224
|
-
const PORT = process.env.PORT ? parseInt(process.env.PORT, 10) : 3000;
|
|
225
|
-
|
|
226
|
-
server.start({
|
|
227
|
-
httpStream: { port: PORT },
|
|
228
|
-
transportType: "httpStream",
|
|
229
|
-
});
|
|
230
|
-
|
|
231
|
-
console.log(`
|
|
232
|
-
🚀 Session Context Demo server running on HTTP Stream!
|
|
233
|
-
|
|
234
|
-
Try these endpoints:
|
|
235
|
-
- MCP: http://localhost:${PORT}/mcp
|
|
236
|
-
- Health: http://localhost:${PORT}/health
|
|
237
|
-
|
|
238
|
-
Test with curl:
|
|
239
|
-
curl -H "Authorization: Bearer admin-token" \\
|
|
240
|
-
-H "Content-Type: application/json" \\
|
|
241
|
-
-d '{"method":"tools/call","params":{"name":"whoami","arguments":{}}}' \\
|
|
242
|
-
http://localhost:${PORT}/mcp
|
|
243
|
-
`);
|
|
244
|
-
} else {
|
|
245
|
-
server.start({ transportType: "stdio" });
|
|
246
|
-
|
|
247
|
-
console.log(`
|
|
248
|
-
🚀 Session Context Demo server started with stdio transport!
|
|
249
|
-
|
|
250
|
-
Environment variables for authentication:
|
|
251
|
-
- USER_ID=${process.env.USER_ID || "(not set - will use 'default-user')"}
|
|
252
|
-
- USERNAME=${process.env.USERNAME || "(not set - will use 'Anonymous')"}
|
|
253
|
-
- USER_ROLE=${process.env.USER_ROLE || "(not set - will use 'guest')"}
|
|
254
|
-
|
|
255
|
-
To test with different user roles:
|
|
256
|
-
USER_ID=admin001 USERNAME="John Admin" USER_ROLE=admin npx fastmcp dev src/examples/session-context.ts
|
|
257
|
-
|
|
258
|
-
Available tools:
|
|
259
|
-
- whoami: Get current user info
|
|
260
|
-
- admin-operation: Test admin permissions
|
|
261
|
-
- check-permissions: Check specific permissions
|
|
262
|
-
|
|
263
|
-
Available resources:
|
|
264
|
-
- session://current-user: Current user JSON data
|
|
265
|
-
|
|
266
|
-
Available prompts:
|
|
267
|
-
- personalized-greeting: Get a personalized greeting
|
|
268
|
-
`);
|
|
269
|
-
}
|