@opentiny/next-sdk 0.3.0-alpha.0 → 0.3.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.
@@ -23900,6 +23900,81 @@ Set the \`cycles\` parameter to \`"ref"\` to resolve cyclical schemas with defs.
23900
23900
  const createMessageChannelPairTransport = () => createTransportPair();
23901
23901
  const isMessageChannelServerTransport = (transport) => transport instanceof MessageChannelServerTransport;
23902
23902
  const isMcpServer = (server) => server instanceof McpServer;
23903
+ const setupBuiltinProxy = (transport) => {
23904
+ transport.onmessage = async (message) => {
23905
+ if (!message || typeof message !== "object") return;
23906
+ const id2 = message.id;
23907
+ try {
23908
+ if (message.method === "initialize") {
23909
+ await transport.send({
23910
+ jsonrpc: "2.0",
23911
+ id: id2,
23912
+ result: {
23913
+ protocolVersion: "2024-11-05",
23914
+ capabilities: { tools: {} },
23915
+ serverInfo: { name: "browser-builtin-webmcp-proxy", version: "1.0.0" }
23916
+ }
23917
+ });
23918
+ } else if (message.method === "notifications/initialized") {
23919
+ } else if (message.method === "ping" || message.method === "custom_ping") {
23920
+ await transport.send({ jsonrpc: "2.0", id: id2, result: {} });
23921
+ } else if (message.method === "tools/list") {
23922
+ const nativeCtx = typeof navigator !== "undefined" ? navigator.modelContextTesting || navigator.modelContext : null;
23923
+ if (nativeCtx && nativeCtx.listTools) {
23924
+ const rawTools = await nativeCtx.listTools();
23925
+ const tools = rawTools.map((t) => {
23926
+ let schemaObj = {};
23927
+ if (typeof t.inputSchema === "string") {
23928
+ try {
23929
+ schemaObj = JSON.parse(t.inputSchema);
23930
+ } catch (e) {
23931
+ console.error("Failed to parse inputSchema:", e);
23932
+ }
23933
+ } else if (typeof t.inputSchema === "object" && t.inputSchema !== null) {
23934
+ schemaObj = t.inputSchema;
23935
+ }
23936
+ return {
23937
+ name: t.name,
23938
+ description: t.description || "",
23939
+ inputSchema: {
23940
+ type: "object",
23941
+ properties: schemaObj.properties || {},
23942
+ required: schemaObj.required || []
23943
+ }
23944
+ };
23945
+ });
23946
+ await transport.send({ jsonrpc: "2.0", id: id2, result: { tools } });
23947
+ } else {
23948
+ await transport.send({ jsonrpc: "2.0", id: id2, result: { tools: [] } });
23949
+ }
23950
+ } else if (message.method === "tools/call") {
23951
+ const nativeCtx = typeof navigator !== "undefined" ? navigator.modelContextTesting || navigator.modelContext : null;
23952
+ if (nativeCtx && nativeCtx.executeTool) {
23953
+ const { name, arguments: args } = message.params;
23954
+ const result = await nativeCtx.executeTool(name, JSON.stringify(args || {}));
23955
+ const finalResult = result && typeof result === "object" && "content" in result ? result : { content: [{ type: "text", text: typeof result === "string" ? result : JSON.stringify(result) }] };
23956
+ await transport.send({ jsonrpc: "2.0", id: id2, result: finalResult });
23957
+ } else {
23958
+ await transport.send({
23959
+ jsonrpc: "2.0",
23960
+ id: id2,
23961
+ error: { code: -32601, message: "Browser built-in WebMCP not available" }
23962
+ });
23963
+ }
23964
+ } else if (id2 !== void 0) {
23965
+ await transport.send({
23966
+ jsonrpc: "2.0",
23967
+ id: id2,
23968
+ error: { code: -32601, message: `Method not found: ${message.method}` }
23969
+ });
23970
+ }
23971
+ } catch (err) {
23972
+ if (id2 !== void 0) {
23973
+ await transport.send({ jsonrpc: "2.0", id: id2, error: { code: -32e3, message: err.message || String(err) } });
23974
+ }
23975
+ }
23976
+ };
23977
+ };
23903
23978
  class WebMcpClient {
23904
23979
  constructor(clientInfo, options) {
23905
23980
  const info = {
@@ -23931,7 +24006,7 @@ Set the \`cycles\` parameter to \`"ref"\` to resolve cyclical schemas with defs.
23931
24006
  await this.client.connect(this.transport);
23932
24007
  return { transport: this.transport, sessionId: this.transport.sessionId };
23933
24008
  }
23934
- const { url: url2, token, sessionId, type: type2, agent, onError } = options;
24009
+ const { url: url2, token, sessionId, type: type2, agent, builtin, onError } = options;
23935
24010
  if (agent === true) {
23936
24011
  const proxyOptions = { client: this.client, url: url2, token, sessionId };
23937
24012
  let response;
@@ -23940,6 +24015,9 @@ Set the \`cycles\` parameter to \`"ref"\` to resolve cyclical schemas with defs.
23940
24015
  transport2.onerror = async (error) => {
23941
24016
  onError?.(error);
23942
24017
  };
24018
+ if (builtin === true) {
24019
+ setupBuiltinProxy(transport2);
24020
+ }
23943
24021
  response = { transport: transport2, sessionId: sessionId2 };
23944
24022
  };
23945
24023
  await connectProxy();
@@ -23896,6 +23896,81 @@ const createMessageChannelServerTransport = (endpoint, globalObject) => new Mess
23896
23896
  const createMessageChannelPairTransport = () => createTransportPair();
23897
23897
  const isMessageChannelServerTransport = (transport) => transport instanceof MessageChannelServerTransport;
23898
23898
  const isMcpServer = (server) => server instanceof McpServer;
23899
+ const setupBuiltinProxy = (transport) => {
23900
+ transport.onmessage = async (message) => {
23901
+ if (!message || typeof message !== "object") return;
23902
+ const id2 = message.id;
23903
+ try {
23904
+ if (message.method === "initialize") {
23905
+ await transport.send({
23906
+ jsonrpc: "2.0",
23907
+ id: id2,
23908
+ result: {
23909
+ protocolVersion: "2024-11-05",
23910
+ capabilities: { tools: {} },
23911
+ serverInfo: { name: "browser-builtin-webmcp-proxy", version: "1.0.0" }
23912
+ }
23913
+ });
23914
+ } else if (message.method === "notifications/initialized") {
23915
+ } else if (message.method === "ping" || message.method === "custom_ping") {
23916
+ await transport.send({ jsonrpc: "2.0", id: id2, result: {} });
23917
+ } else if (message.method === "tools/list") {
23918
+ const nativeCtx = typeof navigator !== "undefined" ? navigator.modelContextTesting || navigator.modelContext : null;
23919
+ if (nativeCtx && nativeCtx.listTools) {
23920
+ const rawTools = await nativeCtx.listTools();
23921
+ const tools = rawTools.map((t) => {
23922
+ let schemaObj = {};
23923
+ if (typeof t.inputSchema === "string") {
23924
+ try {
23925
+ schemaObj = JSON.parse(t.inputSchema);
23926
+ } catch (e) {
23927
+ console.error("Failed to parse inputSchema:", e);
23928
+ }
23929
+ } else if (typeof t.inputSchema === "object" && t.inputSchema !== null) {
23930
+ schemaObj = t.inputSchema;
23931
+ }
23932
+ return {
23933
+ name: t.name,
23934
+ description: t.description || "",
23935
+ inputSchema: {
23936
+ type: "object",
23937
+ properties: schemaObj.properties || {},
23938
+ required: schemaObj.required || []
23939
+ }
23940
+ };
23941
+ });
23942
+ await transport.send({ jsonrpc: "2.0", id: id2, result: { tools } });
23943
+ } else {
23944
+ await transport.send({ jsonrpc: "2.0", id: id2, result: { tools: [] } });
23945
+ }
23946
+ } else if (message.method === "tools/call") {
23947
+ const nativeCtx = typeof navigator !== "undefined" ? navigator.modelContextTesting || navigator.modelContext : null;
23948
+ if (nativeCtx && nativeCtx.executeTool) {
23949
+ const { name, arguments: args } = message.params;
23950
+ const result = await nativeCtx.executeTool(name, JSON.stringify(args || {}));
23951
+ const finalResult = result && typeof result === "object" && "content" in result ? result : { content: [{ type: "text", text: typeof result === "string" ? result : JSON.stringify(result) }] };
23952
+ await transport.send({ jsonrpc: "2.0", id: id2, result: finalResult });
23953
+ } else {
23954
+ await transport.send({
23955
+ jsonrpc: "2.0",
23956
+ id: id2,
23957
+ error: { code: -32601, message: "Browser built-in WebMCP not available" }
23958
+ });
23959
+ }
23960
+ } else if (id2 !== void 0) {
23961
+ await transport.send({
23962
+ jsonrpc: "2.0",
23963
+ id: id2,
23964
+ error: { code: -32601, message: `Method not found: ${message.method}` }
23965
+ });
23966
+ }
23967
+ } catch (err) {
23968
+ if (id2 !== void 0) {
23969
+ await transport.send({ jsonrpc: "2.0", id: id2, error: { code: -32e3, message: err.message || String(err) } });
23970
+ }
23971
+ }
23972
+ };
23973
+ };
23899
23974
  class WebMcpClient {
23900
23975
  constructor(clientInfo, options) {
23901
23976
  const info = {
@@ -23927,7 +24002,7 @@ class WebMcpClient {
23927
24002
  await this.client.connect(this.transport);
23928
24003
  return { transport: this.transport, sessionId: this.transport.sessionId };
23929
24004
  }
23930
- const { url: url2, token, sessionId, type: type2, agent, onError } = options;
24005
+ const { url: url2, token, sessionId, type: type2, agent, builtin, onError } = options;
23931
24006
  if (agent === true) {
23932
24007
  const proxyOptions = { client: this.client, url: url2, token, sessionId };
23933
24008
  let response;
@@ -23936,6 +24011,9 @@ class WebMcpClient {
23936
24011
  transport2.onerror = async (error) => {
23937
24012
  onError?.(error);
23938
24013
  };
24014
+ if (builtin === true) {
24015
+ setupBuiltinProxy(transport2);
24016
+ }
23939
24017
  response = { transport: transport2, sessionId: sessionId2 };
23940
24018
  };
23941
24019
  await connectProxy();