@mcp-ts/sdk 2.3.4 → 2.4.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.
Files changed (89) hide show
  1. package/README.md +161 -39
  2. package/dist/adapters/agui-adapter.d.mts +4 -4
  3. package/dist/adapters/agui-adapter.d.ts +4 -4
  4. package/dist/adapters/agui-middleware.d.mts +4 -4
  5. package/dist/adapters/agui-middleware.d.ts +4 -4
  6. package/dist/adapters/ai-adapter.d.mts +4 -4
  7. package/dist/adapters/ai-adapter.d.ts +4 -4
  8. package/dist/adapters/langchain-adapter.d.mts +4 -4
  9. package/dist/adapters/langchain-adapter.d.ts +4 -4
  10. package/dist/adapters/mastra-adapter.d.mts +2 -2
  11. package/dist/adapters/mastra-adapter.d.ts +2 -2
  12. package/dist/client/index.d.mts +3 -3
  13. package/dist/client/index.d.ts +3 -3
  14. package/dist/client/index.js +2 -2
  15. package/dist/client/index.js.map +1 -1
  16. package/dist/client/index.mjs +2 -2
  17. package/dist/client/index.mjs.map +1 -1
  18. package/dist/client/react.d.mts +11 -10
  19. package/dist/client/react.d.ts +11 -10
  20. package/dist/client/react.js +66 -26
  21. package/dist/client/react.js.map +1 -1
  22. package/dist/client/react.mjs +67 -27
  23. package/dist/client/react.mjs.map +1 -1
  24. package/dist/client/vue.d.mts +8 -7
  25. package/dist/client/vue.d.ts +8 -7
  26. package/dist/client/vue.js +27 -17
  27. package/dist/client/vue.js.map +1 -1
  28. package/dist/client/vue.mjs +27 -17
  29. package/dist/client/vue.mjs.map +1 -1
  30. package/dist/{events-CK3N--3g.d.mts → events-C4m7tK1U.d.mts} +0 -1
  31. package/dist/{events-CK3N--3g.d.ts → events-C4m7tK1U.d.ts} +0 -1
  32. package/dist/{index-CmjMd2ac.d.ts → index-Ch7ouNSa.d.ts} +3 -3
  33. package/dist/{index-Cfjsme-a.d.mts → index-L5XoXgsb.d.mts} +3 -3
  34. package/dist/index.d.mts +6 -6
  35. package/dist/index.d.ts +6 -6
  36. package/dist/index.js +799 -326
  37. package/dist/index.js.map +1 -1
  38. package/dist/index.mjs +793 -324
  39. package/dist/index.mjs.map +1 -1
  40. package/dist/{multi-session-client-C_kPHpD5.d.ts → multi-session-client-Bwm-efqg.d.ts} +43 -38
  41. package/dist/{multi-session-client-C7hGqzgM.d.mts → multi-session-client-k-9RvWvi.d.mts} +43 -38
  42. package/dist/server/index.d.mts +48 -20
  43. package/dist/server/index.d.ts +48 -20
  44. package/dist/server/index.js +791 -320
  45. package/dist/server/index.js.map +1 -1
  46. package/dist/server/index.mjs +787 -319
  47. package/dist/server/index.mjs.map +1 -1
  48. package/dist/shared/index.d.mts +10 -9
  49. package/dist/shared/index.d.ts +10 -9
  50. package/dist/shared/index.js +7 -5
  51. package/dist/shared/index.js.map +1 -1
  52. package/dist/shared/index.mjs +5 -4
  53. package/dist/shared/index.mjs.map +1 -1
  54. package/dist/{tool-router-BMzhoNYt.d.ts → tool-router-CZMrOG8J.d.ts} +1 -1
  55. package/dist/{tool-router-BhHsvBfP.d.mts → tool-router-CuApsDiV.d.mts} +1 -1
  56. package/dist/{types-CxFaaZrM.d.mts → types-DCk_IF4L.d.mts} +5 -3
  57. package/dist/{types-CxFaaZrM.d.ts → types-DCk_IF4L.d.ts} +5 -3
  58. package/migrations/neon/20260513010000_install_mcp_sessions.sql +40 -3
  59. package/migrations/neon/20260513020000_add_session_cleanup_cron.sql +4 -4
  60. package/migrations/supabase/20260330195700_install_mcp_sessions.sql +67 -3
  61. package/migrations/supabase/20260421010000_add_session_cleanup_cron.sql +6 -6
  62. package/migrations/v2.3.4/neon/20260513010000_install_mcp_sessions.sql +69 -0
  63. package/migrations/v2.3.4/neon/20260513020000_add_session_cleanup_cron.sql +35 -0
  64. package/migrations/v2.3.4/supabase/20260330195700_install_mcp_sessions.sql +82 -0
  65. package/migrations/v2.3.4/supabase/20260421010000_add_session_cleanup_cron.sql +32 -0
  66. package/package.json +13 -3
  67. package/src/client/core/sse-client.ts +2 -2
  68. package/src/client/react/index.ts +1 -1
  69. package/src/client/react/oauth-popup.tsx +34 -13
  70. package/src/client/react/use-mcp.ts +19 -45
  71. package/src/client/utils/session-state.ts +43 -0
  72. package/src/client/vue/use-mcp.ts +18 -47
  73. package/src/server/handlers/sse-handler.ts +16 -9
  74. package/src/server/mcp/multi-session-client.ts +48 -11
  75. package/src/server/mcp/oauth-client.ts +133 -111
  76. package/src/server/mcp/storage-oauth-provider.ts +79 -50
  77. package/src/server/storage/file-backend.ts +74 -18
  78. package/src/server/storage/index.ts +2 -4
  79. package/src/server/storage/memory-backend.ts +49 -13
  80. package/src/server/storage/neon-backend.ts +201 -68
  81. package/src/server/storage/redis-backend.ts +81 -23
  82. package/src/server/storage/session-lifecycle.ts +78 -0
  83. package/src/server/storage/sqlite-backend.ts +89 -15
  84. package/src/server/storage/supabase-backend.ts +188 -63
  85. package/src/server/storage/types.ts +49 -16
  86. package/src/shared/constants.ts +5 -6
  87. package/src/shared/events.ts +0 -1
  88. package/src/shared/types.ts +5 -3
  89. package/src/shared/utils.ts +26 -0
@@ -1,5 +1,5 @@
1
1
  import { Tool } from '@modelcontextprotocol/sdk/types.js';
2
- import { u as ToolClientProvider, T as ToolClient } from './types-CxFaaZrM.js';
2
+ import { u as ToolClientProvider, T as ToolClient } from './types-DCk_IF4L.js';
3
3
 
4
4
  /**
5
5
  * ToolIndex — Lightweight in-memory search index for MCP tool discovery.
@@ -1,5 +1,5 @@
1
1
  import { Tool } from '@modelcontextprotocol/sdk/types.js';
2
- import { u as ToolClientProvider, T as ToolClient } from './types-CxFaaZrM.mjs';
2
+ import { u as ToolClientProvider, T as ToolClient } from './types-DCk_IF4L.mjs';
3
3
 
4
4
  /**
5
5
  * ToolIndex — Lightweight in-memory search index for MCP tool discovery.
@@ -83,6 +83,7 @@ type ToolInfo = {
83
83
  outputSchema?: Tool['outputSchema'];
84
84
  };
85
85
  type TransportType = 'sse' | 'streamable-http';
86
+ type SessionStatus = 'pending' | 'active';
86
87
  type McpRpcMethod = 'connect' | 'disconnect' | 'listTools' | 'callTool' | 'listSessions' | 'getSession' | 'finishAuth' | 'listPrompts' | 'getPrompt' | 'listResources' | 'readResource';
87
88
  interface McpRpcRequest {
88
89
  id: string;
@@ -126,7 +127,7 @@ interface ReadResourceParams {
126
127
  uri: string;
127
128
  }
128
129
  interface FinishAuthParams {
129
- sessionId: string;
130
+ state: string;
130
131
  code: string;
131
132
  }
132
133
  type McpRpcParams = ConnectParams | DisconnectParams | SessionParams | CallToolParams | GetPromptParams | ReadResourceParams | FinishAuthParams | undefined;
@@ -137,11 +138,12 @@ interface SessionInfo {
137
138
  serverUrl: string;
138
139
  transport: TransportType;
139
140
  createdAt: number;
141
+ updatedAt?: number;
140
142
  /**
141
143
  * Session readiness for auto-restore.
142
- * false means auth is pending and should be resumed explicitly by user action.
144
+ * `pending` means auth is in progress and should be resumed explicitly by user action.
143
145
  */
144
- active?: boolean;
146
+ status: SessionStatus;
145
147
  }
146
148
  interface SessionListResult {
147
149
  sessions: SessionInfo[];
@@ -83,6 +83,7 @@ type ToolInfo = {
83
83
  outputSchema?: Tool['outputSchema'];
84
84
  };
85
85
  type TransportType = 'sse' | 'streamable-http';
86
+ type SessionStatus = 'pending' | 'active';
86
87
  type McpRpcMethod = 'connect' | 'disconnect' | 'listTools' | 'callTool' | 'listSessions' | 'getSession' | 'finishAuth' | 'listPrompts' | 'getPrompt' | 'listResources' | 'readResource';
87
88
  interface McpRpcRequest {
88
89
  id: string;
@@ -126,7 +127,7 @@ interface ReadResourceParams {
126
127
  uri: string;
127
128
  }
128
129
  interface FinishAuthParams {
129
- sessionId: string;
130
+ state: string;
130
131
  code: string;
131
132
  }
132
133
  type McpRpcParams = ConnectParams | DisconnectParams | SessionParams | CallToolParams | GetPromptParams | ReadResourceParams | FinishAuthParams | undefined;
@@ -137,11 +138,12 @@ interface SessionInfo {
137
138
  serverUrl: string;
138
139
  transport: TransportType;
139
140
  createdAt: number;
141
+ updatedAt?: number;
140
142
  /**
141
143
  * Session readiness for auto-restore.
142
- * false means auth is pending and should be resumed explicitly by user action.
144
+ * `pending` means auth is in progress and should be resumed explicitly by user action.
143
145
  */
144
- active?: boolean;
146
+ status: SessionStatus;
145
147
  }
146
148
  interface SessionListResult {
147
149
  sessions: SessionInfo[];
@@ -12,17 +12,38 @@ CREATE TABLE IF NOT EXISTS public.mcp_sessions (
12
12
  callback_url TEXT NOT NULL,
13
13
  created_at TIMESTAMPTZ NOT NULL DEFAULT now(),
14
14
  updated_at TIMESTAMPTZ NOT NULL DEFAULT now(),
15
- expires_at TIMESTAMPTZ NOT NULL,
16
- active BOOLEAN DEFAULT false,
15
+ expires_at TIMESTAMPTZ,
16
+ status TEXT NOT NULL DEFAULT 'pending'
17
+ CHECK (status IN ('pending', 'active')),
17
18
  headers JSONB,
19
+ auth_url TEXT,
20
+ CONSTRAINT mcp_sessions_user_session_unique
21
+ UNIQUE (user_id, session_id)
22
+ );
23
+
24
+ CREATE TABLE IF NOT EXISTS public.mcp_credentials (
25
+ id UUID PRIMARY KEY DEFAULT gen_random_uuid(),
26
+ session_id TEXT NOT NULL,
27
+ user_id TEXT NOT NULL,
18
28
  client_information JSONB,
19
29
  tokens JSONB,
20
30
  code_verifier TEXT,
21
- client_id TEXT
31
+ client_id TEXT,
32
+ oauth_state JSONB,
33
+ created_at TIMESTAMPTZ NOT NULL DEFAULT now(),
34
+ updated_at TIMESTAMPTZ NOT NULL DEFAULT now(),
35
+ CONSTRAINT mcp_credentials_session_fk
36
+ FOREIGN KEY (user_id, session_id)
37
+ REFERENCES public.mcp_sessions(user_id, session_id)
38
+ ON DELETE CASCADE,
39
+ CONSTRAINT mcp_credentials_user_session_unique
40
+ UNIQUE (user_id, session_id)
22
41
  );
23
42
 
24
43
  CREATE INDEX IF NOT EXISTS idx_mcp_sessions_user_id ON public.mcp_sessions(user_id);
25
44
  CREATE INDEX IF NOT EXISTS idx_mcp_sessions_expires_at ON public.mcp_sessions(expires_at);
45
+ CREATE INDEX IF NOT EXISTS idx_mcp_credentials_user_session
46
+ ON public.mcp_credentials(user_id, session_id);
26
47
 
27
48
  CREATE OR REPLACE FUNCTION public.set_current_timestamp_updated_at()
28
49
  RETURNS TRIGGER AS $$
@@ -39,6 +60,13 @@ BEFORE UPDATE ON public.mcp_sessions
39
60
  FOR EACH ROW
40
61
  EXECUTE FUNCTION public.set_current_timestamp_updated_at();
41
62
 
63
+ DROP TRIGGER IF EXISTS trg_mcp_credentials_updated_at ON public.mcp_credentials;
64
+
65
+ CREATE TRIGGER trg_mcp_credentials_updated_at
66
+ BEFORE UPDATE ON public.mcp_credentials
67
+ FOR EACH ROW
68
+ EXECUTE FUNCTION public.set_current_timestamp_updated_at();
69
+
42
70
  -- Optional production configuration:
43
71
  -- Create a dedicated app role and use its credentials in NEON_DATABASE_URL.
44
72
  -- Replace neondb and the password before running.
@@ -47,6 +75,7 @@ EXECUTE FUNCTION public.set_current_timestamp_updated_at();
47
75
  -- GRANT CONNECT ON DATABASE neondb TO mcp_service_role;
48
76
  -- GRANT USAGE ON SCHEMA public TO mcp_service_role;
49
77
  -- GRANT SELECT, INSERT, UPDATE, DELETE ON public.mcp_sessions TO mcp_service_role;
78
+ -- GRANT SELECT, INSERT, UPDATE, DELETE ON public.mcp_credentials TO mcp_service_role;
50
79
  -- GRANT USAGE ON ALL SEQUENCES IN SCHEMA public TO mcp_service_role;
51
80
 
52
81
  -- Optional RLS configuration:
@@ -60,6 +89,7 @@ EXECUTE FUNCTION public.set_current_timestamp_updated_at();
60
89
  -- GRANT USAGE ON ALL SEQUENCES IN SCHEMA public TO mcp_service_role;
61
90
  --
62
91
  -- ALTER TABLE public.mcp_sessions ENABLE ROW LEVEL SECURITY;
92
+ -- ALTER TABLE public.mcp_credentials ENABLE ROW LEVEL SECURITY;
63
93
  --
64
94
  -- CREATE POLICY mcp_service_role_full_access
65
95
  -- ON public.mcp_sessions
@@ -67,3 +97,10 @@ EXECUTE FUNCTION public.set_current_timestamp_updated_at();
67
97
  -- TO mcp_service_role
68
98
  -- USING (true)
69
99
  -- WITH CHECK (true);
100
+ --
101
+ -- CREATE POLICY mcp_service_role_oauth_full_access
102
+ -- ON public.mcp_credentials
103
+ -- FOR ALL
104
+ -- TO mcp_service_role
105
+ -- USING (true)
106
+ -- WITH CHECK (true);
@@ -18,12 +18,12 @@ WHERE jobname IN (
18
18
  );
19
19
 
20
20
  -- Stage 1: Short-term Transient Purge (every 5 minutes)
21
- -- Removes failed connections, abandoned OAuth flows, and other inactive
22
- -- sessions whose TTL has expired.
21
+ -- Removes abandoned OAuth flows and other transient
22
+ -- sessions whose pending expiration has passed.
23
23
  SELECT cron.schedule(
24
24
  'mcp-cleanup-transient-sessions',
25
25
  '*/5 * * * *',
26
- $$DELETE FROM public.mcp_sessions WHERE expires_at < now() AND active IS NOT TRUE;$$
26
+ $$DELETE FROM public.mcp_sessions WHERE expires_at IS NOT NULL AND expires_at < now() AND status <> 'active';$$
27
27
  );
28
28
 
29
29
  -- Stage 2: Long-term Dormancy Eviction (daily at midnight UTC)
@@ -31,5 +31,5 @@ SELECT cron.schedule(
31
31
  SELECT cron.schedule(
32
32
  'mcp-cleanup-dormant-sessions',
33
33
  '0 0 * * *',
34
- $$DELETE FROM public.mcp_sessions WHERE active = true AND updated_at < now() - interval '30 days';$$
34
+ $$DELETE FROM public.mcp_sessions WHERE status = 'active' AND updated_at < now() - interval '30 days';$$
35
35
  );
@@ -10,19 +10,41 @@ CREATE TABLE IF NOT EXISTS public.mcp_sessions (
10
10
  callback_url TEXT NOT NULL,
11
11
  created_at TIMESTAMPTZ NOT NULL DEFAULT now(),
12
12
  updated_at TIMESTAMPTZ NOT NULL DEFAULT now(),
13
- expires_at TIMESTAMPTZ NOT NULL,
14
- active BOOLEAN DEFAULT false,
13
+ expires_at TIMESTAMPTZ,
14
+ status TEXT NOT NULL DEFAULT 'pending'
15
+ CHECK (status IN ('pending', 'active')),
15
16
  headers JSONB,
17
+ auth_url TEXT,
18
+ CONSTRAINT mcp_sessions_user_session_unique
19
+ UNIQUE (user_id, session_id)
20
+ );
21
+
22
+ -- Runtime credentials are kept separate from connection metadata.
23
+ CREATE TABLE IF NOT EXISTS public.mcp_credentials (
24
+ id UUID PRIMARY KEY DEFAULT gen_random_uuid(),
25
+ session_id TEXT NOT NULL,
26
+ user_id TEXT NOT NULL,
16
27
  client_information JSONB,
17
28
  tokens JSONB,
18
29
  code_verifier TEXT,
19
- client_id TEXT
30
+ client_id TEXT,
31
+ oauth_state JSONB,
32
+ created_at TIMESTAMPTZ NOT NULL DEFAULT now(),
33
+ updated_at TIMESTAMPTZ NOT NULL DEFAULT now(),
34
+ CONSTRAINT mcp_credentials_session_fk
35
+ FOREIGN KEY (user_id, session_id)
36
+ REFERENCES public.mcp_sessions(user_id, session_id)
37
+ ON DELETE CASCADE,
38
+ CONSTRAINT mcp_credentials_user_session_unique
39
+ UNIQUE (user_id, session_id)
20
40
  );
21
41
 
22
42
  -- Add an index on user_id for faster lookups
23
43
  CREATE INDEX IF NOT EXISTS idx_mcp_sessions_user_id ON public.mcp_sessions(user_id);
24
44
  -- Add an index on expires_at to speed up the cleanup job
25
45
  CREATE INDEX IF NOT EXISTS idx_mcp_sessions_expires_at ON public.mcp_sessions(expires_at);
46
+ CREATE INDEX IF NOT EXISTS idx_mcp_credentials_user_session
47
+ ON public.mcp_credentials(user_id, session_id);
26
48
 
27
49
  -- Trigger to automatically update the 'updated_at' column
28
50
  CREATE OR REPLACE FUNCTION public.set_current_timestamp_updated_at()
@@ -39,8 +61,15 @@ BEFORE UPDATE ON public.mcp_sessions
39
61
  FOR EACH ROW
40
62
  EXECUTE FUNCTION public.set_current_timestamp_updated_at();
41
63
 
64
+ DROP TRIGGER IF EXISTS trg_mcp_credentials_updated_at ON public.mcp_credentials;
65
+ CREATE TRIGGER trg_mcp_credentials_updated_at
66
+ BEFORE UPDATE ON public.mcp_credentials
67
+ FOR EACH ROW
68
+ EXECUTE FUNCTION public.set_current_timestamp_updated_at();
69
+
42
70
  -- Enable Row Level Security (RLS)
43
71
  ALTER TABLE public.mcp_sessions ENABLE ROW LEVEL SECURITY;
72
+ ALTER TABLE public.mcp_credentials ENABLE ROW LEVEL SECURITY;
44
73
 
45
74
  -- Policy 1: Users can read their own sessions
46
75
  CREATE POLICY "Users can view their own sessions"
@@ -51,6 +80,14 @@ USING (
51
80
  auth.uid()::text = user_id
52
81
  );
53
82
 
83
+ CREATE POLICY "Users can view their own credentials"
84
+ ON public.mcp_credentials
85
+ FOR SELECT
86
+ TO authenticated
87
+ USING (
88
+ auth.uid()::text = user_id
89
+ );
90
+
54
91
  -- Policy 2: Users can insert their own sessions
55
92
  CREATE POLICY "Users can insert their own sessions"
56
93
  ON public.mcp_sessions
@@ -60,6 +97,14 @@ WITH CHECK (
60
97
  auth.uid()::text = user_id
61
98
  );
62
99
 
100
+ CREATE POLICY "Users can insert their own credentials"
101
+ ON public.mcp_credentials
102
+ FOR INSERT
103
+ TO authenticated
104
+ WITH CHECK (
105
+ auth.uid()::text = user_id
106
+ );
107
+
63
108
  -- Policy 3: Users can update their own sessions
64
109
  CREATE POLICY "Users can update their own sessions"
65
110
  ON public.mcp_sessions
@@ -72,6 +117,17 @@ WITH CHECK (
72
117
  auth.uid()::text = user_id
73
118
  );
74
119
 
120
+ CREATE POLICY "Users can update their own credentials"
121
+ ON public.mcp_credentials
122
+ FOR UPDATE
123
+ TO authenticated
124
+ USING (
125
+ auth.uid()::text = user_id
126
+ )
127
+ WITH CHECK (
128
+ auth.uid()::text = user_id
129
+ );
130
+
75
131
  -- Policy 4: Users can delete their own sessions
76
132
  CREATE POLICY "Users can delete their own sessions"
77
133
  ON public.mcp_sessions
@@ -80,3 +136,11 @@ TO authenticated
80
136
  USING (
81
137
  auth.uid()::text = user_id
82
138
  );
139
+
140
+ CREATE POLICY "Users can delete their own credentials"
141
+ ON public.mcp_credentials
142
+ FOR DELETE
143
+ TO authenticated
144
+ USING (
145
+ auth.uid()::text = user_id
146
+ );
@@ -5,27 +5,27 @@ CREATE EXTENSION IF NOT EXISTS pg_cron;
5
5
  -- ─────────────────────────────────────────────────────────────────────────────
6
6
  -- Stage 1: Short-term Transient Purge (every 5 minutes)
7
7
  -- ─────────────────────────────────────────────────────────────────────────────
8
- -- Targets sessions that are NOT active (failed connections, abandoned OAuth
9
- -- flows, mid-flow errors) whose TTL has expired. Active sessions are explicitly
8
+ -- Targets transient sessions (pending OAuth or setup states) whose
9
+ -- expiration has passed. Active sessions are explicitly
10
10
  -- excluded from this sweep to preserve automation credentials.
11
11
  --
12
12
  -- The idx_mcp_sessions_expires_at index ensures this is a fast indexed scan.
13
13
  SELECT cron.schedule(
14
14
  'cleanup-transient-sessions',
15
15
  '*/5 * * * *',
16
- $$DELETE FROM public.mcp_sessions WHERE expires_at < now() AND active IS NOT TRUE;$$
16
+ $$DELETE FROM public.mcp_sessions WHERE expires_at IS NOT NULL AND expires_at < now() AND status <> 'active';$$
17
17
  );
18
18
 
19
19
  -- ─────────────────────────────────────────────────────────────────────────────
20
20
  -- Stage 2: Long-term Dormancy Eviction (daily at midnight UTC)
21
21
  -- ─────────────────────────────────────────────────────────────────────────────
22
- -- Safety net for sessions that were successfully established (active = true)
23
- -- but have been completely untouched for 30+ days. This prevents "active"
22
+ -- Safety net for sessions that were successfully established (status = 'active')
23
+ -- but have been completely untouched for 30+ days. This prevents active
24
24
  -- records from persisting indefinitely if they are genuinely abandoned.
25
25
  SELECT cron.schedule(
26
26
  'cleanup-dormant-sessions',
27
27
  '0 0 * * *',
28
- $$DELETE FROM public.mcp_sessions WHERE active = true AND updated_at < now() - interval '30 days';$$
28
+ $$DELETE FROM public.mcp_sessions WHERE status = 'active' AND updated_at < now() - interval '30 days';$$
29
29
  );
30
30
 
31
31
  -- Add a comment on the extension for visibility in Supabase Dashboard
@@ -0,0 +1,69 @@
1
+ -- Create the mcp_sessions table for Neon Postgres.
2
+ -- Run this with an owner/admin role, then grant app access with the least-privilege SQL below.
3
+
4
+ CREATE TABLE IF NOT EXISTS public.mcp_sessions (
5
+ id UUID PRIMARY KEY DEFAULT gen_random_uuid(),
6
+ session_id TEXT NOT NULL UNIQUE,
7
+ user_id TEXT NOT NULL,
8
+ server_id TEXT,
9
+ server_name TEXT,
10
+ server_url TEXT NOT NULL,
11
+ transport_type TEXT NOT NULL,
12
+ callback_url TEXT NOT NULL,
13
+ created_at TIMESTAMPTZ NOT NULL DEFAULT now(),
14
+ updated_at TIMESTAMPTZ NOT NULL DEFAULT now(),
15
+ expires_at TIMESTAMPTZ NOT NULL,
16
+ active BOOLEAN DEFAULT false,
17
+ headers JSONB,
18
+ client_information JSONB,
19
+ tokens JSONB,
20
+ code_verifier TEXT,
21
+ client_id TEXT
22
+ );
23
+
24
+ CREATE INDEX IF NOT EXISTS idx_mcp_sessions_user_id ON public.mcp_sessions(user_id);
25
+ CREATE INDEX IF NOT EXISTS idx_mcp_sessions_expires_at ON public.mcp_sessions(expires_at);
26
+
27
+ CREATE OR REPLACE FUNCTION public.set_current_timestamp_updated_at()
28
+ RETURNS TRIGGER AS $$
29
+ BEGIN
30
+ NEW.updated_at = now();
31
+ RETURN NEW;
32
+ END;
33
+ $$ LANGUAGE plpgsql;
34
+
35
+ DROP TRIGGER IF EXISTS trg_mcp_sessions_updated_at ON public.mcp_sessions;
36
+
37
+ CREATE TRIGGER trg_mcp_sessions_updated_at
38
+ BEFORE UPDATE ON public.mcp_sessions
39
+ FOR EACH ROW
40
+ EXECUTE FUNCTION public.set_current_timestamp_updated_at();
41
+
42
+ -- Optional production configuration:
43
+ -- Create a dedicated app role and use its credentials in NEON_DATABASE_URL.
44
+ -- Replace neondb and the password before running.
45
+ --
46
+ -- CREATE ROLE mcp_service_role LOGIN PASSWORD 'replace-with-a-strong-password';
47
+ -- GRANT CONNECT ON DATABASE neondb TO mcp_service_role;
48
+ -- GRANT USAGE ON SCHEMA public TO mcp_service_role;
49
+ -- GRANT SELECT, INSERT, UPDATE, DELETE ON public.mcp_sessions TO mcp_service_role;
50
+ -- GRANT USAGE ON ALL SEQUENCES IN SCHEMA public TO mcp_service_role;
51
+
52
+ -- Optional RLS configuration:
53
+ -- Uncomment and run this block after creating mcp_service_role if you want
54
+ -- to enforce access through Row Level Security for the dedicated app role.
55
+ --
56
+ -- REVOKE ALL ON public.mcp_sessions FROM PUBLIC;
57
+ -- REVOKE ALL ON ALL SEQUENCES IN SCHEMA public FROM PUBLIC;
58
+ --
59
+ -- GRANT SELECT, INSERT, UPDATE, DELETE ON public.mcp_sessions TO mcp_service_role;
60
+ -- GRANT USAGE ON ALL SEQUENCES IN SCHEMA public TO mcp_service_role;
61
+ --
62
+ -- ALTER TABLE public.mcp_sessions ENABLE ROW LEVEL SECURITY;
63
+ --
64
+ -- CREATE POLICY mcp_service_role_full_access
65
+ -- ON public.mcp_sessions
66
+ -- FOR ALL
67
+ -- TO mcp_service_role
68
+ -- USING (true)
69
+ -- WITH CHECK (true);
@@ -0,0 +1,35 @@
1
+ -- Optional Neon pg_cron cleanup jobs.
2
+ --
3
+ -- Neon pg_cron requires endpoint-level setup before this migration can run:
4
+ -- configure cron.database_name for your compute endpoint, restart the compute,
5
+ -- then install/schedule jobs from the target database.
6
+ --
7
+ -- If pg_cron is not enabled for your Neon project, skip this migration and run
8
+ -- storage.cleanupExpiredSessions() from your application scheduler instead.
9
+
10
+ CREATE EXTENSION IF NOT EXISTS pg_cron;
11
+
12
+ -- Keep reruns idempotent without NOTICE noise.
13
+ SELECT cron.unschedule(jobname)
14
+ FROM cron.job
15
+ WHERE jobname IN (
16
+ 'mcp-cleanup-transient-sessions',
17
+ 'mcp-cleanup-dormant-sessions'
18
+ );
19
+
20
+ -- Stage 1: Short-term Transient Purge (every 5 minutes)
21
+ -- Removes failed connections, abandoned OAuth flows, and other inactive
22
+ -- sessions whose TTL has expired.
23
+ SELECT cron.schedule(
24
+ 'mcp-cleanup-transient-sessions',
25
+ '*/5 * * * *',
26
+ $$DELETE FROM public.mcp_sessions WHERE expires_at < now() AND active IS NOT TRUE;$$
27
+ );
28
+
29
+ -- Stage 2: Long-term Dormancy Eviction (daily at midnight UTC)
30
+ -- Removes active sessions that have not been touched for 30+ days.
31
+ SELECT cron.schedule(
32
+ 'mcp-cleanup-dormant-sessions',
33
+ '0 0 * * *',
34
+ $$DELETE FROM public.mcp_sessions WHERE active = true AND updated_at < now() - interval '30 days';$$
35
+ );
@@ -0,0 +1,82 @@
1
+ -- Create the mcp_sessions table
2
+ CREATE TABLE IF NOT EXISTS public.mcp_sessions (
3
+ id UUID PRIMARY KEY DEFAULT gen_random_uuid(),
4
+ session_id TEXT NOT NULL UNIQUE,
5
+ user_id TEXT NOT NULL, -- Will store the application user's ID
6
+ server_id TEXT,
7
+ server_name TEXT,
8
+ server_url TEXT NOT NULL,
9
+ transport_type TEXT NOT NULL,
10
+ callback_url TEXT NOT NULL,
11
+ created_at TIMESTAMPTZ NOT NULL DEFAULT now(),
12
+ updated_at TIMESTAMPTZ NOT NULL DEFAULT now(),
13
+ expires_at TIMESTAMPTZ NOT NULL,
14
+ active BOOLEAN DEFAULT false,
15
+ headers JSONB,
16
+ client_information JSONB,
17
+ tokens JSONB,
18
+ code_verifier TEXT,
19
+ client_id TEXT
20
+ );
21
+
22
+ -- Add an index on user_id for faster lookups
23
+ CREATE INDEX IF NOT EXISTS idx_mcp_sessions_user_id ON public.mcp_sessions(user_id);
24
+ -- Add an index on expires_at to speed up the cleanup job
25
+ CREATE INDEX IF NOT EXISTS idx_mcp_sessions_expires_at ON public.mcp_sessions(expires_at);
26
+
27
+ -- Trigger to automatically update the 'updated_at' column
28
+ CREATE OR REPLACE FUNCTION public.set_current_timestamp_updated_at()
29
+ RETURNS TRIGGER AS $$
30
+ BEGIN
31
+ NEW.updated_at = now();
32
+ RETURN NEW;
33
+ END;
34
+ $$ LANGUAGE plpgsql;
35
+
36
+ DROP TRIGGER IF EXISTS trg_mcp_sessions_updated_at ON public.mcp_sessions;
37
+ CREATE TRIGGER trg_mcp_sessions_updated_at
38
+ BEFORE UPDATE ON public.mcp_sessions
39
+ FOR EACH ROW
40
+ EXECUTE FUNCTION public.set_current_timestamp_updated_at();
41
+
42
+ -- Enable Row Level Security (RLS)
43
+ ALTER TABLE public.mcp_sessions ENABLE ROW LEVEL SECURITY;
44
+
45
+ -- Policy 1: Users can read their own sessions
46
+ CREATE POLICY "Users can view their own sessions"
47
+ ON public.mcp_sessions
48
+ FOR SELECT
49
+ TO authenticated
50
+ USING (
51
+ auth.uid()::text = user_id
52
+ );
53
+
54
+ -- Policy 2: Users can insert their own sessions
55
+ CREATE POLICY "Users can insert their own sessions"
56
+ ON public.mcp_sessions
57
+ FOR INSERT
58
+ TO authenticated
59
+ WITH CHECK (
60
+ auth.uid()::text = user_id
61
+ );
62
+
63
+ -- Policy 3: Users can update their own sessions
64
+ CREATE POLICY "Users can update their own sessions"
65
+ ON public.mcp_sessions
66
+ FOR UPDATE
67
+ TO authenticated
68
+ USING (
69
+ auth.uid()::text = user_id
70
+ )
71
+ WITH CHECK (
72
+ auth.uid()::text = user_id
73
+ );
74
+
75
+ -- Policy 4: Users can delete their own sessions
76
+ CREATE POLICY "Users can delete their own sessions"
77
+ ON public.mcp_sessions
78
+ FOR DELETE
79
+ TO authenticated
80
+ USING (
81
+ auth.uid()::text = user_id
82
+ );
@@ -0,0 +1,32 @@
1
+ -- Enable the pg_cron extension (available on all Supabase plans).
2
+ -- This is idempotent and safe to run multiple times.
3
+ CREATE EXTENSION IF NOT EXISTS pg_cron;
4
+
5
+ -- -----------------------------------------------------------------------------
6
+ -- Stage 1: Short-term Transient Purge (every 5 minutes)
7
+ -- -----------------------------------------------------------------------------
8
+ -- Targets sessions that are NOT active (failed connections, abandoned OAuth
9
+ -- flows, mid-flow errors) whose TTL has expired. Active sessions are explicitly
10
+ -- excluded from this sweep to preserve automation credentials.
11
+ --
12
+ -- The idx_mcp_sessions_expires_at index ensures this is a fast indexed scan.
13
+ SELECT cron.schedule(
14
+ 'cleanup-transient-sessions',
15
+ '*/5 * * * *',
16
+ $$DELETE FROM public.mcp_sessions WHERE expires_at < now() AND active IS NOT TRUE;$$
17
+ );
18
+
19
+ -- -----------------------------------------------------------------------------
20
+ -- Stage 2: Long-term Dormancy Eviction (daily at midnight UTC)
21
+ -- -----------------------------------------------------------------------------
22
+ -- Safety net for sessions that were successfully established (active = true)
23
+ -- but have been completely untouched for 30+ days. This prevents "active"
24
+ -- records from persisting indefinitely if they are genuinely abandoned.
25
+ SELECT cron.schedule(
26
+ 'cleanup-dormant-sessions',
27
+ '0 0 * * *',
28
+ $$DELETE FROM public.mcp_sessions WHERE active = true AND updated_at < now() - interval '30 days';$$
29
+ );
30
+
31
+ -- Add a comment on the extension for visibility in Supabase Dashboard
32
+ COMMENT ON EXTENSION pg_cron IS 'Automated Session Lifecycle Management.';
package/package.json CHANGED
@@ -1,10 +1,10 @@
1
1
  {
2
2
  "name": "@mcp-ts/sdk",
3
- "version": "2.3.4",
3
+ "version": "2.4.1",
4
4
  "publishConfig": {
5
5
  "access": "public"
6
6
  },
7
- "description": "A lightweight MCP (Model Context Protocol) client library for JavaScript and cross-runtime environments, supporting MCP Apps in host applications and multiple storage backends (Memory, File, Redis, Supabase, Neon).",
7
+ "description": "A lightweight MCP client library for TypeScript with durable sessions across Redis, Supabase, Neon, and SQLite, cross-runtime agent support, and dynamic tool discovery to reduce LLM context usage.",
8
8
  "main": "./dist/index.js",
9
9
  "module": "./dist/index.mjs",
10
10
  "types": "./dist/index.d.ts",
@@ -102,7 +102,17 @@
102
102
  "react",
103
103
  "hook",
104
104
  "anthropic",
105
- "claude"
105
+ "claude",
106
+ "typescript",
107
+ "agent",
108
+ "agents",
109
+ "durable-sessions",
110
+ "tool-discovery",
111
+ "tool-router",
112
+ "context-window",
113
+ "sqlite",
114
+ "supabase",
115
+ "neon"
106
116
  ],
107
117
  "author": "MCP Assistant Contributors",
108
118
  "license": "MIT",
@@ -117,8 +117,8 @@ export class SSEClient {
117
117
  return this.sendRequest<GetSessionResult>('getSession', { sessionId });
118
118
  }
119
119
 
120
- async finishAuth(sessionId: string, code: string): Promise<FinishAuthResult> {
121
- return this.sendRequest<FinishAuthResult>('finishAuth', { sessionId, code });
120
+ async finishAuth(state: string, code: string): Promise<FinishAuthResult> {
121
+ return this.sendRequest<FinishAuthResult>('finishAuth', { state, code });
122
122
  }
123
123
 
124
124
  async listPrompts(sessionId: string): Promise<ListPromptsResult> {
@@ -8,7 +8,7 @@ export { useMcp, type UseMcpOptions, type McpClient, type McpConnection } from '
8
8
 
9
9
  // Optional OAuth popup conveniences. These are not required for auth:
10
10
  // consumers can still provide their own onRedirect handler, callback page UI,
11
- // or complete `finishAuth(sessionId, code)` from a normal redirect flow.
11
+ // or complete `finishAuth(state, code)` from a normal redirect flow.
12
12
  export {
13
13
  useMcpOAuthPopup,
14
14
  openCenteredPopup,