@decocms/mesh-sdk 1.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.
@@ -0,0 +1,128 @@
1
+ import { Client } from "@modelcontextprotocol/sdk/client/index.js";
2
+ import {
3
+ useQuery,
4
+ useSuspenseQuery,
5
+ type UseQueryOptions,
6
+ type UseQueryResult,
7
+ type UseSuspenseQueryOptions,
8
+ type UseSuspenseQueryResult,
9
+ } from "@tanstack/react-query";
10
+ import type {
11
+ ListResourcesResult,
12
+ ReadResourceResult,
13
+ } from "@modelcontextprotocol/sdk/types.js";
14
+ import { KEYS } from "../lib/query-keys";
15
+
16
+ /**
17
+ * List resources from an MCP client.
18
+ * This is the raw async function that can be used outside of React hooks.
19
+ */
20
+ export async function listResources(
21
+ client: Client,
22
+ ): Promise<ListResourcesResult> {
23
+ const capabilities = client.getServerCapabilities();
24
+ if (!capabilities?.resources) {
25
+ return { resources: [] };
26
+ }
27
+ return await client.listResources();
28
+ }
29
+
30
+ /**
31
+ * Read a specific resource from an MCP client.
32
+ * This is the raw async function that can be used outside of React hooks.
33
+ */
34
+ export async function readResource(
35
+ client: Client,
36
+ uri: string,
37
+ ): Promise<ReadResourceResult> {
38
+ return await client.readResource({ uri });
39
+ }
40
+
41
+ export interface UseMcpResourcesListOptions
42
+ extends Omit<
43
+ UseSuspenseQueryOptions<ListResourcesResult, Error>,
44
+ "queryKey" | "queryFn"
45
+ > {
46
+ /** The MCP client from useMCPClient */
47
+ client: Client;
48
+ }
49
+
50
+ /**
51
+ * Suspense hook to list resources from an MCP client.
52
+ * Must be used within a Suspense boundary.
53
+ */
54
+ export function useMCPResourcesList({
55
+ client,
56
+ ...queryOptions
57
+ }: UseMcpResourcesListOptions): UseSuspenseQueryResult<
58
+ ListResourcesResult,
59
+ Error
60
+ > {
61
+ return useSuspenseQuery<ListResourcesResult, Error>({
62
+ ...queryOptions,
63
+ queryKey: KEYS.mcpResourcesList(client),
64
+ queryFn: () => listResources(client),
65
+ staleTime: queryOptions.staleTime ?? 30000,
66
+ retry: false,
67
+ });
68
+ }
69
+
70
+ export interface UseMcpResourcesListQueryOptions
71
+ extends Omit<
72
+ UseQueryOptions<ListResourcesResult, Error>,
73
+ "queryKey" | "queryFn"
74
+ > {
75
+ /** The MCP client from useMCPClient */
76
+ client: Client;
77
+ }
78
+
79
+ /**
80
+ * Non-suspense hook to list resources from an MCP client.
81
+ */
82
+ export function useMCPResourcesListQuery({
83
+ client,
84
+ ...queryOptions
85
+ }: UseMcpResourcesListQueryOptions): UseQueryResult<
86
+ ListResourcesResult,
87
+ Error
88
+ > {
89
+ return useQuery<ListResourcesResult, Error>({
90
+ ...queryOptions,
91
+ queryKey: KEYS.mcpResourcesList(client),
92
+ queryFn: () => listResources(client),
93
+ staleTime: queryOptions.staleTime ?? 30000,
94
+ retry: false,
95
+ });
96
+ }
97
+
98
+ export interface UseMcpReadResourceOptions
99
+ extends Omit<
100
+ UseSuspenseQueryOptions<ReadResourceResult, Error>,
101
+ "queryKey" | "queryFn"
102
+ > {
103
+ /** The MCP client from useMCPClient */
104
+ client: Client;
105
+ /** Resource URI to read */
106
+ uri: string;
107
+ }
108
+
109
+ /**
110
+ * Suspense hook to read a specific resource from an MCP client.
111
+ * Must be used within a Suspense boundary.
112
+ */
113
+ export function useMCPReadResource({
114
+ client,
115
+ uri,
116
+ ...queryOptions
117
+ }: UseMcpReadResourceOptions): UseSuspenseQueryResult<
118
+ ReadResourceResult,
119
+ Error
120
+ > {
121
+ return useSuspenseQuery<ReadResourceResult, Error>({
122
+ ...queryOptions,
123
+ queryKey: KEYS.mcpReadResource(client, uri),
124
+ queryFn: () => readResource(client, uri),
125
+ staleTime: queryOptions.staleTime ?? 30000,
126
+ retry: false,
127
+ });
128
+ }
@@ -0,0 +1,184 @@
1
+ import { Client } from "@modelcontextprotocol/sdk/client/index.js";
2
+ import {
3
+ useMutation,
4
+ useQuery,
5
+ useSuspenseQuery,
6
+ type UseMutationOptions,
7
+ type UseMutationResult,
8
+ type UseQueryOptions,
9
+ type UseQueryResult,
10
+ type UseSuspenseQueryOptions,
11
+ type UseSuspenseQueryResult,
12
+ } from "@tanstack/react-query";
13
+ import type {
14
+ CallToolRequest,
15
+ CallToolResult,
16
+ ListToolsResult,
17
+ } from "@modelcontextprotocol/sdk/types.js";
18
+ import { KEYS } from "../lib/query-keys";
19
+
20
+ export interface UseMcpToolsListOptions
21
+ extends Omit<
22
+ UseSuspenseQueryOptions<ListToolsResult, Error>,
23
+ "queryKey" | "queryFn"
24
+ > {
25
+ /** The MCP client from useMCPClient */
26
+ client: Client;
27
+ }
28
+
29
+ /**
30
+ * Suspense hook to list tools from an MCP client.
31
+ * Must be used within a Suspense boundary.
32
+ */
33
+ export function useMCPToolsList({
34
+ client,
35
+ ...queryOptions
36
+ }: UseMcpToolsListOptions): UseSuspenseQueryResult<ListToolsResult, Error> {
37
+ return useSuspenseQuery<ListToolsResult, Error>({
38
+ ...queryOptions,
39
+ queryKey: KEYS.mcpToolsList(client),
40
+ queryFn: async () => {
41
+ return await client.listTools();
42
+ },
43
+ staleTime: queryOptions.staleTime ?? 30000,
44
+ retry: false,
45
+ });
46
+ }
47
+
48
+ export interface UseMcpToolsListQueryOptions
49
+ extends Omit<
50
+ UseQueryOptions<ListToolsResult, Error>,
51
+ "queryKey" | "queryFn"
52
+ > {
53
+ /** The MCP client from useMCPClient */
54
+ client: Client;
55
+ }
56
+
57
+ /**
58
+ * Non-suspense hook to list tools from an MCP client.
59
+ */
60
+ export function useMCPToolsListQuery({
61
+ client,
62
+ ...queryOptions
63
+ }: UseMcpToolsListQueryOptions): UseQueryResult<ListToolsResult, Error> {
64
+ return useQuery<ListToolsResult, Error>({
65
+ ...queryOptions,
66
+ queryKey: KEYS.mcpToolsList(client),
67
+ queryFn: async () => {
68
+ return await client.listTools();
69
+ },
70
+ staleTime: queryOptions.staleTime ?? 30000,
71
+ retry: false,
72
+ });
73
+ }
74
+
75
+ export interface UseMcpToolCallOptions<TData = CallToolResult>
76
+ extends Omit<
77
+ UseSuspenseQueryOptions<CallToolResult, Error, TData>,
78
+ "queryKey" | "queryFn"
79
+ > {
80
+ /** The MCP client from useMCPClient */
81
+ client: Client;
82
+ /** Tool name to call */
83
+ toolName: string;
84
+ /** Tool arguments */
85
+ toolArguments?: Record<string, unknown>;
86
+ }
87
+
88
+ /**
89
+ * Suspense hook to call a tool on an MCP client.
90
+ * Must be used within a Suspense boundary.
91
+ *
92
+ * @template TData - The type of data returned (after optional select transformation)
93
+ */
94
+ export function useMCPToolCall<TData = CallToolResult>({
95
+ client,
96
+ toolName,
97
+ toolArguments,
98
+ ...queryOptions
99
+ }: UseMcpToolCallOptions<TData>): UseSuspenseQueryResult<TData, Error> {
100
+ const argsKey = JSON.stringify(toolArguments ?? {});
101
+
102
+ return useSuspenseQuery<CallToolResult, Error, TData>({
103
+ ...queryOptions,
104
+ queryKey: KEYS.mcpToolCall(client, toolName, argsKey),
105
+ queryFn: async () => {
106
+ const result = await client.callTool({
107
+ name: toolName,
108
+ arguments: toolArguments,
109
+ });
110
+ return result as CallToolResult;
111
+ },
112
+ staleTime: queryOptions.staleTime ?? 30000,
113
+ retry: false,
114
+ });
115
+ }
116
+
117
+ export interface UseMcpToolCallQueryOptions<TData = CallToolResult>
118
+ extends Omit<
119
+ UseQueryOptions<CallToolResult, Error, TData>,
120
+ "queryKey" | "queryFn"
121
+ > {
122
+ /** The MCP client from useMCPClient */
123
+ client: Client;
124
+ /** Tool name to call */
125
+ toolName: string;
126
+ /** Tool arguments */
127
+ toolArguments?: Record<string, unknown>;
128
+ }
129
+
130
+ /**
131
+ * Non-suspense hook to call a tool on an MCP client.
132
+ *
133
+ * @template TData - The type of data returned (after optional select transformation)
134
+ */
135
+ export function useMCPToolCallQuery<TData = CallToolResult>({
136
+ client,
137
+ toolName,
138
+ toolArguments,
139
+ ...queryOptions
140
+ }: UseMcpToolCallQueryOptions<TData>): UseQueryResult<TData, Error> {
141
+ const argsKey = JSON.stringify(toolArguments ?? {});
142
+
143
+ return useQuery<CallToolResult, Error, TData>({
144
+ ...queryOptions,
145
+ queryKey: KEYS.mcpToolCall(client, toolName, argsKey),
146
+ queryFn: async () => {
147
+ const result = await client.callTool({
148
+ name: toolName,
149
+ arguments: toolArguments,
150
+ });
151
+ return result as CallToolResult;
152
+ },
153
+ staleTime: queryOptions.staleTime ?? 30000,
154
+ retry: false,
155
+ });
156
+ }
157
+
158
+ export interface UseMcpToolCallMutationOptions
159
+ extends Omit<
160
+ UseMutationOptions<CallToolResult, Error, CallToolRequest["params"]>,
161
+ "mutationFn"
162
+ > {
163
+ /** The MCP client from useMCPClient */
164
+ client: Client;
165
+ }
166
+
167
+ /**
168
+ * Mutation hook to call a tool on an MCP client.
169
+ */
170
+ export function useMCPToolCallMutation({
171
+ client,
172
+ ...mutationOptions
173
+ }: UseMcpToolCallMutationOptions): UseMutationResult<
174
+ CallToolResult,
175
+ Error,
176
+ CallToolRequest["params"]
177
+ > {
178
+ return useMutation<CallToolResult, Error, CallToolRequest["params"]>({
179
+ ...mutationOptions,
180
+ mutationFn: async (params) => {
181
+ return (await client.callTool(params)) as CallToolResult;
182
+ },
183
+ });
184
+ }
@@ -0,0 +1,91 @@
1
+ /**
2
+ * Virtual MCP Collection Hooks
3
+ *
4
+ * Provides React hooks for working with virtual MCPs using React Query.
5
+ * These hooks offer a reactive interface for accessing and manipulating virtual MCPs.
6
+ */
7
+
8
+ import type { VirtualMCPEntity } from "../types/virtual-mcp";
9
+ import { useProjectContext } from "../context";
10
+ import {
11
+ useCollectionActions,
12
+ useCollectionItem,
13
+ useCollectionList,
14
+ type CollectionFilter,
15
+ type UseCollectionListOptions,
16
+ } from "./use-collections";
17
+ import { useMCPClient } from "./use-mcp-client";
18
+ import { SELF_MCP_ALIAS_ID } from "../lib/constants";
19
+
20
+ /**
21
+ * Filter definition for virtual MCPs (matches @deco/ui Filter shape)
22
+ */
23
+ export type VirtualMCPFilter = CollectionFilter;
24
+
25
+ /**
26
+ * Options for useVirtualMCPs hook
27
+ */
28
+ export type UseVirtualMCPsOptions = UseCollectionListOptions<VirtualMCPEntity>;
29
+
30
+ /**
31
+ * Hook to get all virtual MCPs
32
+ *
33
+ * @param options - Filter and configuration options
34
+ * @returns Suspense query result with virtual MCPs as VirtualMCPEntity[]
35
+ */
36
+ export function useVirtualMCPs(options: UseVirtualMCPsOptions = {}) {
37
+ const { org } = useProjectContext();
38
+ const client = useMCPClient({
39
+ connectionId: SELF_MCP_ALIAS_ID,
40
+ orgId: org.id,
41
+ });
42
+
43
+ return useCollectionList<VirtualMCPEntity>(
44
+ org.id,
45
+ "VIRTUAL_MCP",
46
+ client,
47
+ options,
48
+ );
49
+ }
50
+
51
+ /**
52
+ * Hook to get a single virtual MCP by ID
53
+ *
54
+ * @param virtualMcpId - The ID of the virtual MCP to fetch (null/undefined for default virtual MCP)
55
+ * @returns VirtualMCPEntity | null - null means use default virtual MCP
56
+ */
57
+ export function useVirtualMCP(
58
+ virtualMcpId: string | null | undefined,
59
+ ): VirtualMCPEntity | null {
60
+ const { org } = useProjectContext();
61
+ const client = useMCPClient({
62
+ connectionId: SELF_MCP_ALIAS_ID,
63
+ orgId: org.id,
64
+ });
65
+
66
+ // If null/undefined, return null (use default virtual MCP)
67
+ // Use collection item hook for database virtual MCPs
68
+ const dbVirtualMCP = useCollectionItem<VirtualMCPEntity>(
69
+ org.id,
70
+ "VIRTUAL_MCP",
71
+ virtualMcpId ?? undefined,
72
+ client,
73
+ );
74
+
75
+ return dbVirtualMCP;
76
+ }
77
+
78
+ /**
79
+ * Hook to get virtual MCP mutation actions (create, update, delete)
80
+ *
81
+ * @returns Object with create, update, and delete mutation hooks
82
+ */
83
+ export function useVirtualMCPActions() {
84
+ const { org } = useProjectContext();
85
+ const client = useMCPClient({
86
+ connectionId: SELF_MCP_ALIAS_ID,
87
+ orgId: org.id,
88
+ });
89
+
90
+ return useCollectionActions<VirtualMCPEntity>(org.id, "VIRTUAL_MCP", client);
91
+ }
package/src/index.ts ADDED
@@ -0,0 +1,128 @@
1
+ // Context
2
+ export {
3
+ ProjectContextProvider,
4
+ useProjectContext,
5
+ Locator,
6
+ ORG_ADMIN_PROJECT_SLUG,
7
+ type ProjectContextProviderProps,
8
+ type ProjectLocator,
9
+ type LocatorStructured,
10
+ } from "./context";
11
+
12
+ // Hooks
13
+ export {
14
+ // Collection hooks
15
+ useCollectionItem,
16
+ useCollectionList,
17
+ useCollectionActions,
18
+ type CollectionEntity,
19
+ type CollectionFilter,
20
+ type UseCollectionListOptions,
21
+ // Connection hooks
22
+ useConnections,
23
+ useConnection,
24
+ useConnectionActions,
25
+ type ConnectionFilter,
26
+ type UseConnectionsOptions,
27
+ // MCP client hook and factory
28
+ createMCPClient,
29
+ useMCPClient,
30
+ type CreateMcpClientOptions,
31
+ type UseMcpClientOptions,
32
+ // MCP tools hooks
33
+ useMCPToolsList,
34
+ useMCPToolsListQuery,
35
+ useMCPToolCall,
36
+ useMCPToolCallQuery,
37
+ useMCPToolCallMutation,
38
+ type UseMcpToolsListOptions,
39
+ type UseMcpToolsListQueryOptions,
40
+ type UseMcpToolCallOptions,
41
+ type UseMcpToolCallQueryOptions,
42
+ type UseMcpToolCallMutationOptions,
43
+ // MCP resources hooks and helpers
44
+ listResources,
45
+ readResource,
46
+ useMCPResourcesList,
47
+ useMCPResourcesListQuery,
48
+ useMCPReadResource,
49
+ type UseMcpResourcesListOptions,
50
+ type UseMcpResourcesListQueryOptions,
51
+ type UseMcpReadResourceOptions,
52
+ // MCP prompts hooks and helpers
53
+ listPrompts,
54
+ getPrompt,
55
+ useMCPPromptsList,
56
+ useMCPPromptsListQuery,
57
+ useMCPGetPrompt,
58
+ type UseMcpPromptsListOptions,
59
+ type UseMcpPromptsListQueryOptions,
60
+ type UseMcpGetPromptOptions,
61
+ // Virtual MCP hooks
62
+ useVirtualMCPs,
63
+ useVirtualMCP,
64
+ useVirtualMCPActions,
65
+ type VirtualMCPFilter,
66
+ type UseVirtualMCPsOptions,
67
+ } from "./hooks";
68
+
69
+ // Types
70
+ export {
71
+ ConnectionEntitySchema,
72
+ ConnectionCreateDataSchema,
73
+ ConnectionUpdateDataSchema,
74
+ isStdioParameters,
75
+ parseVirtualUrl,
76
+ buildVirtualUrl,
77
+ type ConnectionEntity,
78
+ type ConnectionCreateData,
79
+ type ConnectionUpdateData,
80
+ type ConnectionParameters,
81
+ type HttpConnectionParameters,
82
+ type StdioConnectionParameters,
83
+ type OAuthConfig,
84
+ type ToolDefinition,
85
+ // Virtual MCP types
86
+ VirtualMCPEntitySchema,
87
+ VirtualMCPCreateDataSchema,
88
+ VirtualMCPUpdateDataSchema,
89
+ type VirtualMCPEntity,
90
+ type VirtualMCPCreateData,
91
+ type VirtualMCPUpdateData,
92
+ type VirtualMCPConnection,
93
+ type ToolSelectionMode,
94
+ } from "./types";
95
+
96
+ // Streamable HTTP transport
97
+ export { StreamableHTTPClientTransport } from "./lib/streamable-http-client-transport";
98
+
99
+ // Query keys
100
+ export { KEYS } from "./lib/query-keys";
101
+
102
+ // MCP OAuth utilities
103
+ export {
104
+ authenticateMcp,
105
+ handleOAuthCallback,
106
+ isConnectionAuthenticated,
107
+ type McpOAuthProviderOptions,
108
+ type OAuthTokenInfo,
109
+ type AuthenticateMcpResult,
110
+ type McpAuthStatus,
111
+ type OAuthWindowMode,
112
+ } from "./lib/mcp-oauth";
113
+
114
+ // Constants and well-known MCP definitions
115
+ export {
116
+ // Frontend self MCP ID
117
+ SELF_MCP_ALIAS_ID,
118
+ // Org-scoped MCP ID generators
119
+ WellKnownOrgMCPId,
120
+ // Connection factory functions
121
+ getWellKnownRegistryConnection,
122
+ getWellKnownCommunityRegistryConnection,
123
+ getWellKnownSelfConnection,
124
+ getWellKnownOpenRouterConnection,
125
+ getWellKnownMcpStudioConnection,
126
+ // Virtual MCP factory functions
127
+ getWellKnownDecopilotAgent,
128
+ } from "./lib/constants";