@tmcp/session-manager 0.1.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/LICENSE ADDED
@@ -0,0 +1,21 @@
1
+ MIT License
2
+
3
+ Copyright (c) 2025 Paolo Ricciuti
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining a copy
6
+ of this software and associated documentation files (the "Software"), to deal
7
+ in the Software without restriction, including without limitation the rights
8
+ to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9
+ copies of the Software, and to permit persons to whom the Software is
10
+ furnished to do so, subject to the following conditions:
11
+
12
+ The above copyright notice and this permission notice shall be included in all
13
+ copies or substantial portions of the Software.
14
+
15
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16
+ IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17
+ FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18
+ AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19
+ LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20
+ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
21
+ SOFTWARE.
package/README.md ADDED
@@ -0,0 +1,150 @@
1
+ # @tmcp/session-manager
2
+
3
+ Session management for TMCP (TypeScript Model Context Protocol) transport implementations. This package provides the base `SessionManager` interface and an in-memory implementation for managing client sessions in MCP servers.
4
+
5
+ ## Installation
6
+
7
+ ```bash
8
+ pnpm add @tmcp/session-manager
9
+ ```
10
+
11
+ ## Overview
12
+
13
+ Session managers handle the storage and communication with client sessions in TMCP transport layers. They manage:
14
+
15
+ - **Session Creation**: Establishing new client sessions with stream controllers
16
+ - **Session Deletion**: Cleaning up disconnected sessions
17
+ - **Session Queries**: Checking if sessions exist
18
+ - **Message Broadcasting**: Sending messages to specific sessions or all sessions
19
+
20
+ ## Usage
21
+
22
+ ### In-Memory Session Manager (Default)
23
+
24
+ The `InMemorySessionManager` stores sessions in memory and is suitable for single-server deployments:
25
+
26
+ ```javascript
27
+ import { InMemorySessionManager } from '@tmcp/session-manager';
28
+
29
+ const sessionManager = new InMemorySessionManager();
30
+ ```
31
+
32
+ ### Custom Session Manager
33
+
34
+ You can implement your own session manager by extending the base `SessionManager` class:
35
+
36
+ ```javascript
37
+ import { SessionManager } from '@tmcp/session-manager';
38
+
39
+ class CustomSessionManager extends SessionManager {
40
+ /**
41
+ * Create a new session with the given ID and controller
42
+ * @param {string} id - Session ID
43
+ * @param {ReadableStreamDefaultController} controller - Stream controller
44
+ */
45
+ create(id, controller) {
46
+ // Implementation here
47
+ }
48
+
49
+ /**
50
+ * Delete a session by ID
51
+ * @param {string} id - Session ID to delete
52
+ */
53
+ delete(id) {
54
+ // Implementation here
55
+ }
56
+
57
+ /**
58
+ * Check if a session exists
59
+ * @param {string} id - Session ID to check
60
+ * @returns {Promise<boolean>} - Whether the session exists
61
+ */
62
+ async has(id) {
63
+ // Implementation here
64
+ }
65
+
66
+ /**
67
+ * Send data to specific sessions or all sessions
68
+ * @param {string[] | undefined} sessions - Session IDs to send to (undefined = all sessions)
69
+ * @param {string} data - Data to send
70
+ */
71
+ send(sessions, data) {
72
+ // Implementation here
73
+ }
74
+ }
75
+ ```
76
+
77
+ ## API
78
+
79
+ ### `SessionManager` (Abstract Base Class)
80
+
81
+ #### Methods
82
+
83
+ ##### `create(id: string, controller: ReadableStreamDefaultController): void`
84
+
85
+ Creates a new session with the specified ID and associates it with a stream controller.
86
+
87
+ **Parameters:**
88
+
89
+ - `id` - Unique session identifier
90
+ - `controller` - ReadableStreamDefaultController for sending data to the client
91
+
92
+ ##### `delete(id: string): void`
93
+
94
+ Removes a session and cleans up its resources.
95
+
96
+ **Parameters:**
97
+
98
+ - `id` - Session ID to delete
99
+
100
+ ##### `has(id: string): Promise<boolean>`
101
+
102
+ Checks whether a session with the given ID exists.
103
+
104
+ **Parameters:**
105
+
106
+ - `id` - Session ID to check
107
+
108
+ **Returns:**
109
+
110
+ - Promise resolving to true if the session exists, false otherwise
111
+
112
+ ##### `send(sessions: string[] | undefined, data: string): void`
113
+
114
+ Sends data to one or more sessions.
115
+
116
+ **Parameters:**
117
+
118
+ - `sessions` - Array of session IDs to send to, or undefined to send to all sessions
119
+ - `data` - String data to send (typically SSE-formatted)
120
+
121
+ ### `InMemorySessionManager`
122
+
123
+ Concrete implementation that stores sessions in a JavaScript Map. Suitable for single-server deployments.
124
+
125
+ #### Usage with Transport Layers
126
+
127
+ ```javascript
128
+ import { HttpTransport } from '@tmcp/transport-http';
129
+ import { SseTransport } from '@tmcp/transport-sse';
130
+ import { InMemorySessionManager } from '@tmcp/session-manager';
131
+
132
+ const sessionManager = new InMemorySessionManager();
133
+
134
+ // Use with HTTP transport
135
+ const httpTransport = new HttpTransport(server, { sessionManager });
136
+
137
+ // Use with SSE transport
138
+ const sseTransport = new SseTransport(server, { sessionManager });
139
+ ```
140
+
141
+ ## Related Packages
142
+
143
+ - [`@tmcp/session-manager-redis`](../session-manager-redis) - Redis-based session manager for multi-server deployments
144
+ - [`@tmcp/transport-http`](../transport-http) - HTTP transport using session managers
145
+ - [`@tmcp/transport-sse`](../transport-sse) - SSE transport using session managers
146
+ - [`tmcp`](../tmcp) - Core TMCP server implementation
147
+
148
+ ## License
149
+
150
+ MIT
package/package.json ADDED
@@ -0,0 +1,38 @@
1
+ {
2
+ "name": "@tmcp/session-manager",
3
+ "version": "0.1.0",
4
+ "description": "",
5
+ "main": "src/index.js",
6
+ "repository": {
7
+ "type": "git",
8
+ "url": "git+https://github.com/paoloricciuti/tmcp.git",
9
+ "directory": "packages/session-manager"
10
+ },
11
+ "keywords": [
12
+ "tmcp",
13
+ "http",
14
+ "transport",
15
+ "session",
16
+ "manager"
17
+ ],
18
+ "types": "./src/types/index.d.ts",
19
+ "exports": {
20
+ ".": {
21
+ "types": "./src/types/index.d.ts",
22
+ "import": "./src/index.js"
23
+ }
24
+ },
25
+ "type": "module",
26
+ "publishConfig": {
27
+ "access": "public"
28
+ },
29
+ "devDependencies": {
30
+ "dts-buddy": "^0.6.2",
31
+ "publint": "^0.3.12",
32
+ "@tmcp/internal-ts-helpers": "^1.0.0"
33
+ },
34
+ "scripts": {
35
+ "generate:types": "dts-buddy && node scripts/abstractify.js && publint",
36
+ "prepublish": "pnpm generate:types"
37
+ }
38
+ }
@@ -0,0 +1,3 @@
1
+ import { abstractify } from '@tmcp/internal-ts-helpers';
2
+
3
+ abstractify();
package/src/index.js ADDED
@@ -0,0 +1,89 @@
1
+ /* eslint-disable no-unused-vars */
2
+
3
+ /**
4
+ * @abstract
5
+ */
6
+ export class SessionManager {
7
+ /**
8
+ * @abstract
9
+ * @param {string} id
10
+ * @param {ReadableStreamDefaultController} controller
11
+ */
12
+ create(id, controller) {
13
+ throw new Error('Method not implemented.');
14
+ }
15
+
16
+ /**
17
+ * @abstract
18
+ * @param {string} id
19
+ */
20
+ delete(id) {
21
+ throw new Error('Method not implemented.');
22
+ }
23
+
24
+ /**
25
+ * @abstract
26
+ * @param {string} id
27
+ * @returns {Promise<boolean>}
28
+ */
29
+ has(id) {
30
+ throw new Error('Method not implemented.');
31
+ }
32
+
33
+ /**
34
+ * @abstract
35
+ * @param {string[] | undefined} sessions
36
+ * @param {string} data
37
+ */
38
+ send(sessions, data) {
39
+ throw new Error('Method not implemented.');
40
+ }
41
+ }
42
+
43
+ export class InMemorySessionManager extends SessionManager {
44
+ /**
45
+ * @type {Map<string, ReadableStreamDefaultController>}
46
+ */
47
+ #sessions = new Map();
48
+ #text_encoder = new TextEncoder();
49
+
50
+ /**
51
+ * @param {string} id
52
+ * @param {ReadableStreamDefaultController} controller
53
+ */
54
+ create(id, controller) {
55
+ this.#sessions.set(id, controller);
56
+ }
57
+
58
+ /**
59
+ * @param {string} id
60
+ */
61
+ delete(id) {
62
+ const controller = this.#sessions.get(id);
63
+ if (controller) {
64
+ controller.close();
65
+ this.#sessions.delete(id);
66
+ }
67
+ }
68
+
69
+ /**
70
+ * @abstract
71
+ * @param {string} id
72
+ * @returns {Promise<boolean>}
73
+ */
74
+ async has(id) {
75
+ return this.#sessions.has(id);
76
+ }
77
+
78
+ /**
79
+ * @param {string[] | undefined} sessions
80
+ * @param {string} data
81
+ */
82
+ send(sessions, data) {
83
+ for (const [id, controller] of this.#sessions.entries()) {
84
+ if (sessions == null || sessions.includes(id)) {
85
+ controller.enqueue(this.#text_encoder.encode(data));
86
+ }
87
+ }
88
+ }
89
+ }
@@ -0,0 +1,28 @@
1
+ declare module '@tmcp/session-manager' {
2
+ /**
3
+ * @abstract
4
+ */
5
+ export abstract class SessionManager {
6
+ /**
7
+ * @abstract
8
+ * */
9
+ abstract create(id: string, controller: ReadableStreamDefaultController): void;
10
+ /**
11
+ * @abstract
12
+ * */
13
+ abstract delete(id: string): void;
14
+ /**
15
+ * @abstract
16
+ * */
17
+ abstract has(id: string): Promise<boolean>;
18
+ /**
19
+ * @abstract
20
+ * */
21
+ abstract send(sessions: string[] | undefined, data: string): void;
22
+ }
23
+ export class InMemorySessionManager extends SessionManager {
24
+ #private;
25
+ }
26
+ export {};
27
+ }
28
+ //# sourceMappingURL=index.d.ts.map
@@ -0,0 +1,16 @@
1
+ {
2
+ "version": 3,
3
+ "file": "index.d.ts",
4
+ "names": [
5
+ "SessionManager",
6
+ "InMemorySessionManager"
7
+ ],
8
+ "sources": [
9
+ "../index.js"
10
+ ],
11
+ "sourcesContent": [
12
+ null
13
+ ],
14
+ "mappings": ";;;;cAKaA,cAAcA;;;;;;;;;;;;;;;;;;cAqCdC,sBAAsBA",
15
+ "ignoreList": []
16
+ }
package/tsconfig.json ADDED
@@ -0,0 +1,16 @@
1
+ {
2
+ "compilerOptions": {
3
+ "target": "esnext",
4
+ "module": "nodenext",
5
+ "moduleResolution": "nodenext",
6
+ "allowJs": true,
7
+ "checkJs": true,
8
+ "declaration": true,
9
+ "emitDeclarationOnly": false,
10
+ "strict": true,
11
+ "noEmit": true,
12
+ "skipLibCheck": true
13
+ },
14
+ "include": ["src/**/*"],
15
+ "exclude": ["node_modules"]
16
+ }