@tsed/platform-mcp 8.44.12 → 8.44.14

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.
@@ -56,6 +56,7 @@ export class PlatformMcpGatewayService {
56
56
  async connect(upstream, entry) {
57
57
  const { Client } = await import("@modelcontextprotocol/client");
58
58
  const transport = await createUpstreamTransport(upstream);
59
+ const label = this.getLabel(upstream);
59
60
  let catalog;
60
61
  const invalidate = () => {
61
62
  catalog = undefined;
@@ -87,10 +88,21 @@ export class PlatformMcpGatewayService {
87
88
  return {
88
89
  client,
89
90
  getCatalog: () => {
90
- catalog ||= this.loadCatalog(client).catch((error) => {
91
- catalog = undefined;
92
- throw error;
93
- });
91
+ if (!catalog) {
92
+ const pending = this.loadCatalog(client, label).then(({ catalog: loaded, complete }) => {
93
+ // an incomplete catalog is served once and listed again on the next request
94
+ if (!complete && catalog === pending) {
95
+ catalog = undefined;
96
+ }
97
+ return loaded;
98
+ }, (error) => {
99
+ if (catalog === pending) {
100
+ catalog = undefined;
101
+ }
102
+ throw error;
103
+ });
104
+ catalog = pending;
105
+ }
94
106
  return catalog;
95
107
  },
96
108
  run: async (fn) => {
@@ -105,15 +117,54 @@ export class PlatformMcpGatewayService {
105
117
  }
106
118
  };
107
119
  }
108
- async loadCatalog(client) {
120
+ /**
121
+ * Lists every catalog the upstream advertises.
122
+ *
123
+ * A listing that fails is logged and left empty so the others stay available: an upstream may advertise
124
+ * a capability it cannot serve (e.g. prompts without any prompt configured). The catalog is then flagged
125
+ * incomplete so the next request lists it again. It rejects only when every advertised listing fails.
126
+ */
127
+ async loadCatalog(client, label) {
109
128
  const capabilities = client.getServerCapabilities() || {};
110
- const [tools, resources, resourceTemplates, prompts] = await Promise.all([
111
- listAll(capabilities.tools, (params) => client.listTools(params), "tools"),
112
- listAll(capabilities.resources, (params) => client.listResources(params), "resources"),
113
- listAll(capabilities.resources, (params) => client.listResourceTemplates(params), "resourceTemplates"),
114
- listAll(capabilities.prompts, (params) => client.listPrompts(params), "prompts")
115
- ]);
116
- return { tools, resources, resourceTemplates, prompts };
129
+ const listings = [
130
+ ["tools", capabilities.tools, (params) => client.listTools(params)],
131
+ ["resources", capabilities.resources, (params) => client.listResources(params)],
132
+ ["resourceTemplates", capabilities.resources, (params) => client.listResourceTemplates(params)],
133
+ ["prompts", capabilities.prompts, (params) => client.listPrompts(params)]
134
+ ];
135
+ const results = await Promise.allSettled(listings.map(([key, enabled, list]) => listAll(enabled, list, key)));
136
+ const failures = results.flatMap((result, index) => result.status === "rejected" ? [{ key: listings[index][0], error: result.reason }] : []);
137
+ const advertised = listings.filter(([, enabled]) => enabled).length;
138
+ failures.forEach(({ key, error }) => {
139
+ logger().warn({
140
+ event: "MCP_GATEWAY_CATALOG_ERROR",
141
+ message: `MCP upstream "${label}": unable to list ${key}, they are skipped.`,
142
+ error
143
+ });
144
+ });
145
+ if (failures.length && failures.length === advertised) {
146
+ throw failures[0].error;
147
+ }
148
+ const [tools, resources, resourceTemplates, prompts] = results.map((result) => (result.status === "fulfilled" ? result.value : []));
149
+ return {
150
+ catalog: {
151
+ tools: tools,
152
+ resources: resources,
153
+ resourceTemplates: resourceTemplates,
154
+ prompts: prompts
155
+ },
156
+ complete: !failures.length
157
+ };
158
+ }
159
+ /**
160
+ * Names the upstream in the logs without the credentials its URL may carry (userinfo, query, hash).
161
+ */
162
+ getLabel(upstream) {
163
+ if (upstream.type === "stdio") {
164
+ return upstream.command;
165
+ }
166
+ const { origin, pathname } = new URL(upstream.url);
167
+ return `${origin}${pathname}`;
117
168
  }
118
169
  getPool(upstream) {
119
170
  let pool = this.pools.get(upstream);