opentool 0.19.2 → 0.19.3
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/README.md +6 -0
- package/dist/index.d.ts +3 -14
- package/dist/index.js +124 -437
- package/dist/index.js.map +1 -1
- package/dist/runtime/index.d.ts +16 -0
- package/dist/runtime/index.js +804 -0
- package/dist/runtime/index.js.map +1 -0
- package/package.json +5 -1
- package/templates/base/package.json +1 -1
- package/templates/polymarket-simple-trade/package.json +1 -1
package/README.md
CHANGED
|
@@ -210,6 +210,12 @@ export const mcp = {
|
|
|
210
210
|
|
|
211
211
|
Tools without this export stay HTTP-only, which is useful when you want selective access. Mix and match as needed.
|
|
212
212
|
|
|
213
|
+
If you need the programmatic MCP runtime helpers, import them from `opentool/runtime` instead of the root package:
|
|
214
|
+
|
|
215
|
+
```typescript
|
|
216
|
+
import { createDevServer, createStdioServer } from "opentool/runtime";
|
|
217
|
+
```
|
|
218
|
+
|
|
213
219
|
### Testing with MCP Inspector
|
|
214
220
|
|
|
215
221
|
You can run MCP Inspector against your local tool set:
|
package/dist/index.d.ts
CHANGED
|
@@ -1,6 +1,5 @@
|
|
|
1
|
-
import {
|
|
2
|
-
|
|
3
|
-
export { B as BuildConfig, a as BuildMetadata, C as ConnectedApp, b as CronSpec, G as GetHandler, H as HTTP_METHODS, c as HttpHandlerDefinition, d as HttpMethod, M as McpConfig, e as Metadata, N as NormalizedSchedule, P as PaymentConfig, f as PostHandler, S as ScheduleType, g as ServerConfig, h as TemplatePreviewProfile, i as Tool, j as ToolAsset, k as ToolCategory, l as ToolContent, m as ToolMetadataOverrides, n as ToolModule, o as ToolModuleGET, p as ToolModulePOST, q as ToolProfile } from './index-9Z3wo28l.js';
|
|
1
|
+
import { T as ToolResponse } from './index-9Z3wo28l.js';
|
|
2
|
+
export { B as BuildConfig, a as BuildMetadata, C as ConnectedApp, b as CronSpec, G as GetHandler, H as HTTP_METHODS, c as HttpHandlerDefinition, d as HttpMethod, I as InternalToolDefinition, M as McpConfig, e as Metadata, N as NormalizedSchedule, P as PaymentConfig, f as PostHandler, S as ScheduleType, g as ServerConfig, h as TemplatePreviewProfile, i as Tool, j as ToolAsset, k as ToolCategory, l as ToolContent, m as ToolMetadataOverrides, n as ToolModule, o as ToolModuleGET, p as ToolModulePOST, q as ToolProfile } from './index-9Z3wo28l.js';
|
|
4
3
|
export { C as CurrencySpec, D as DEFAULT_FACILITATOR, a as DefineX402PaymentConfig, P as PAYMENT_HEADERS, R as RequireX402PaymentOptions, b as RequireX402PaymentOutcome, c as RequireX402PaymentSuccess, S as SUPPORTED_CURRENCIES, X as X402FacilitatorConfig, d as X402Payment, e as X402PaymentContext, f as X402PaymentDefinition, g as X402PaymentRequiredError, h as X402VerificationResult, i as defineX402Payment, j as getX402PaymentContext, r as requireX402Payment, w as withX402Payment } from './payment-BLm1ltur.js';
|
|
5
4
|
export { EIP3009Authorization, X402BrowserClient, X402BrowserClientConfig, X402Client, X402ClientConfig, X402PayRequest, X402PayResult, payX402, payX402WithWallet } from './x402/index.js';
|
|
6
5
|
export { DEFAULT_CHAIN, DEFAULT_TOKENS, chains, getRpcUrl, registry, tokens, wallet, walletToolkit } from './wallet/index.js';
|
|
@@ -16,16 +15,6 @@ export { D as DEFAULT_HYPERLIQUID_MARKET_SLIPPAGE_BPS, a as DEFAULT_HYPERLIQUID_
|
|
|
16
15
|
import 'viem';
|
|
17
16
|
import 'viem/accounts';
|
|
18
17
|
|
|
19
|
-
/**
|
|
20
|
-
* Create local development server for MCP tooling.
|
|
21
|
-
*/
|
|
22
|
-
declare function createDevServer(tools: InternalToolDefinition[]): Server;
|
|
23
|
-
/**
|
|
24
|
-
* Create stdio server for use with AWS Lambda MCP Adapter
|
|
25
|
-
*/
|
|
26
|
-
declare function createStdioServer(tools?: InternalToolDefinition[]): Promise<void>;
|
|
27
|
-
declare function resolveRuntimePath(value: string): string;
|
|
28
|
-
|
|
29
18
|
interface CreateMcpAdapterOptions {
|
|
30
19
|
name: string;
|
|
31
20
|
schema?: ZodSchema;
|
|
@@ -38,4 +27,4 @@ interface CreateMcpAdapterOptions {
|
|
|
38
27
|
declare function createMcpAdapter(options: CreateMcpAdapterOptions): (rawArguments: unknown) => Promise<ToolResponse>;
|
|
39
28
|
declare function responseToToolResponse(response: Response): Promise<ToolResponse>;
|
|
40
29
|
|
|
41
|
-
export {
|
|
30
|
+
export { ToolResponse, createMcpAdapter, responseToToolResponse };
|
package/dist/index.js
CHANGED
|
@@ -1,10 +1,3 @@
|
|
|
1
|
-
import { Server } from '@modelcontextprotocol/sdk/server/index.js';
|
|
2
|
-
import { StdioServerTransport } from '@modelcontextprotocol/sdk/server/stdio.js';
|
|
3
|
-
import { ListToolsRequestSchema, CallToolRequestSchema } from '@modelcontextprotocol/sdk/types.js';
|
|
4
|
-
import * as fs from 'fs';
|
|
5
|
-
import * as path from 'path';
|
|
6
|
-
import { fileURLToPath } from 'url';
|
|
7
|
-
import { zodToJsonSchema } from '@alcyone-labs/zod-to-json-schema';
|
|
8
1
|
import { z } from 'zod';
|
|
9
2
|
import { zeroAddress, createWalletClient, http, createPublicClient, parseUnits, encodeFunctionData, erc20Abi, maxUint256, decodeFunctionData } from 'viem';
|
|
10
3
|
import { privateKeyToAccount } from 'viem/accounts';
|
|
@@ -16,12 +9,8 @@ import { keccak_256 } from '@noble/hashes/sha3';
|
|
|
16
9
|
import { hexToBytes, concatBytes, bytesToHex } from '@noble/hashes/utils';
|
|
17
10
|
import { createHmac, randomBytes } from 'crypto';
|
|
18
11
|
|
|
19
|
-
|
|
20
|
-
|
|
21
|
-
}) : x)(function(x) {
|
|
22
|
-
if (typeof require !== "undefined") return require.apply(this, arguments);
|
|
23
|
-
throw Error('Dynamic require of "' + x + '" is not supported');
|
|
24
|
-
});
|
|
12
|
+
// src/types/index.ts
|
|
13
|
+
var HTTP_METHODS = ["GET", "HEAD", "POST", "PUT", "DELETE", "PATCH", "OPTIONS"];
|
|
25
14
|
var X402_VERSION = 1;
|
|
26
15
|
var HEADER_X402 = "X-PAYMENT";
|
|
27
16
|
var HEADER_PAYMENT_RESPONSE = "X-PAYMENT-RESPONSE";
|
|
@@ -483,419 +472,6 @@ function normalizeCurrency(currency) {
|
|
|
483
472
|
function toDecimalString(value) {
|
|
484
473
|
return typeof value === "number" ? value.toString() : value;
|
|
485
474
|
}
|
|
486
|
-
|
|
487
|
-
// src/adapters/mcp.ts
|
|
488
|
-
var HTTP_METHODS = ["GET", "HEAD", "POST", "PUT", "DELETE", "PATCH", "OPTIONS"];
|
|
489
|
-
function createMcpAdapter(options) {
|
|
490
|
-
const normalizedSchema = ensureSchema(options.schema);
|
|
491
|
-
const defaultMethod = resolveDefaultMethod(options);
|
|
492
|
-
const httpHandler = options.httpHandlers[defaultMethod];
|
|
493
|
-
if (!httpHandler) {
|
|
494
|
-
throw new Error(`Tool "${options.name}" does not export an HTTP handler for ${defaultMethod}`);
|
|
495
|
-
}
|
|
496
|
-
return async function invoke(rawArguments) {
|
|
497
|
-
const validated = normalizedSchema ? normalizedSchema.parse(rawArguments ?? {}) : rawArguments;
|
|
498
|
-
const request = buildRequest(options.name, defaultMethod, validated);
|
|
499
|
-
try {
|
|
500
|
-
const response = await Promise.resolve(httpHandler(request));
|
|
501
|
-
return await responseToToolResponse(response);
|
|
502
|
-
} catch (error) {
|
|
503
|
-
if (error instanceof X402PaymentRequiredError) {
|
|
504
|
-
return await responseToToolResponse(error.response);
|
|
505
|
-
}
|
|
506
|
-
throw error;
|
|
507
|
-
}
|
|
508
|
-
};
|
|
509
|
-
}
|
|
510
|
-
function resolveDefaultMethod(options) {
|
|
511
|
-
const explicit = options.defaultMethod?.toUpperCase();
|
|
512
|
-
if (explicit && typeof options.httpHandlers[explicit] === "function") {
|
|
513
|
-
return explicit;
|
|
514
|
-
}
|
|
515
|
-
const preferredOrder = ["POST", "PUT", "PATCH", "GET", "DELETE", "OPTIONS", "HEAD"];
|
|
516
|
-
for (const method of preferredOrder) {
|
|
517
|
-
if (typeof options.httpHandlers[method] === "function") {
|
|
518
|
-
return method;
|
|
519
|
-
}
|
|
520
|
-
}
|
|
521
|
-
const available = Object.keys(options.httpHandlers).filter(
|
|
522
|
-
(method) => typeof options.httpHandlers[method] === "function"
|
|
523
|
-
);
|
|
524
|
-
if (available.length > 0) {
|
|
525
|
-
return available[0];
|
|
526
|
-
}
|
|
527
|
-
throw new Error(`No HTTP handlers available for tool "${options.name}"`);
|
|
528
|
-
}
|
|
529
|
-
function ensureSchema(schema) {
|
|
530
|
-
if (!schema) {
|
|
531
|
-
return void 0;
|
|
532
|
-
}
|
|
533
|
-
if (schema instanceof z.ZodType) {
|
|
534
|
-
return schema;
|
|
535
|
-
}
|
|
536
|
-
if (typeof schema?.parse === "function") {
|
|
537
|
-
return schema;
|
|
538
|
-
}
|
|
539
|
-
throw new Error("MCP adapter requires a valid Zod schema to validate arguments");
|
|
540
|
-
}
|
|
541
|
-
function buildRequest(name, method, params) {
|
|
542
|
-
const url = new URL(`https://opentool.local/${encodeURIComponent(name)}`);
|
|
543
|
-
const headers = new Headers({
|
|
544
|
-
"x-opentool-invocation": "mcp",
|
|
545
|
-
"x-opentool-tool": name
|
|
546
|
-
});
|
|
547
|
-
if (method === "GET" || method === "HEAD") {
|
|
548
|
-
if (params && typeof params === "object") {
|
|
549
|
-
Object.entries(params).forEach(([key, value]) => {
|
|
550
|
-
if (value == null) {
|
|
551
|
-
return;
|
|
552
|
-
}
|
|
553
|
-
url.searchParams.set(key, String(value));
|
|
554
|
-
});
|
|
555
|
-
}
|
|
556
|
-
return new Request(url, { method, headers });
|
|
557
|
-
}
|
|
558
|
-
headers.set("Content-Type", "application/json");
|
|
559
|
-
const init = { method, headers };
|
|
560
|
-
if (params != null) {
|
|
561
|
-
init.body = JSON.stringify(params);
|
|
562
|
-
}
|
|
563
|
-
return new Request(url, init);
|
|
564
|
-
}
|
|
565
|
-
async function responseToToolResponse(response) {
|
|
566
|
-
const statusIsError = response.status >= 400;
|
|
567
|
-
const contentType = response.headers.get("content-type") ?? "";
|
|
568
|
-
const text = await response.text();
|
|
569
|
-
if (contentType.includes("application/json")) {
|
|
570
|
-
try {
|
|
571
|
-
const payload = text ? JSON.parse(text) : {};
|
|
572
|
-
if (payload && typeof payload === "object" && Array.isArray(payload.content)) {
|
|
573
|
-
return {
|
|
574
|
-
content: payload.content,
|
|
575
|
-
isError: payload.isError ?? statusIsError
|
|
576
|
-
};
|
|
577
|
-
}
|
|
578
|
-
return {
|
|
579
|
-
content: [{ type: "text", text: JSON.stringify(payload, null, 2) }],
|
|
580
|
-
isError: statusIsError
|
|
581
|
-
};
|
|
582
|
-
} catch {
|
|
583
|
-
return {
|
|
584
|
-
content: [{ type: "text", text }],
|
|
585
|
-
isError: statusIsError
|
|
586
|
-
};
|
|
587
|
-
}
|
|
588
|
-
}
|
|
589
|
-
if (!text) {
|
|
590
|
-
return {
|
|
591
|
-
content: [],
|
|
592
|
-
isError: statusIsError
|
|
593
|
-
};
|
|
594
|
-
}
|
|
595
|
-
return {
|
|
596
|
-
content: [{ type: "text", text }],
|
|
597
|
-
isError: statusIsError
|
|
598
|
-
};
|
|
599
|
-
}
|
|
600
|
-
|
|
601
|
-
// src/runtime/index.ts
|
|
602
|
-
function createDevServer(tools) {
|
|
603
|
-
const metadata = loadMetadata();
|
|
604
|
-
const metadataMap = buildMetadataMap(metadata);
|
|
605
|
-
const adapters = buildAdapters(tools);
|
|
606
|
-
const server = new Server(
|
|
607
|
-
{
|
|
608
|
-
name: "opentool-dev",
|
|
609
|
-
version: "1.0.0"
|
|
610
|
-
},
|
|
611
|
-
{
|
|
612
|
-
capabilities: {
|
|
613
|
-
tools: {}
|
|
614
|
-
}
|
|
615
|
-
}
|
|
616
|
-
);
|
|
617
|
-
server.setRequestHandler(ListToolsRequestSchema, async () => ({
|
|
618
|
-
tools: adapters.map(({ tool }) => serializeTool(tool, metadataMap))
|
|
619
|
-
}));
|
|
620
|
-
server.setRequestHandler(CallToolRequestSchema, async (request) => {
|
|
621
|
-
const entry = adapters.find(({ tool }) => {
|
|
622
|
-
const toolName = tool.metadata?.name || tool.filename;
|
|
623
|
-
return toolName === request.params.name;
|
|
624
|
-
});
|
|
625
|
-
if (!entry) {
|
|
626
|
-
throw new Error(`Tool ${request.params.name} not found or not MCP-enabled`);
|
|
627
|
-
}
|
|
628
|
-
try {
|
|
629
|
-
return await entry.invoke(request.params.arguments);
|
|
630
|
-
} catch (error) {
|
|
631
|
-
const message = error && error.message || String(error);
|
|
632
|
-
return {
|
|
633
|
-
content: [{ type: "text", text: `Error: ${message}` }],
|
|
634
|
-
isError: true
|
|
635
|
-
};
|
|
636
|
-
}
|
|
637
|
-
});
|
|
638
|
-
return server;
|
|
639
|
-
}
|
|
640
|
-
async function createStdioServer(tools) {
|
|
641
|
-
const metadata = loadMetadata();
|
|
642
|
-
const metadataMap = buildMetadataMap(metadata);
|
|
643
|
-
const toolDefinitions = tools || await loadToolsFromDirectory(metadataMap);
|
|
644
|
-
const adapters = buildAdapters(toolDefinitions);
|
|
645
|
-
const server = new Server(
|
|
646
|
-
{
|
|
647
|
-
name: "opentool-runtime",
|
|
648
|
-
version: "1.0.0"
|
|
649
|
-
},
|
|
650
|
-
{
|
|
651
|
-
capabilities: {
|
|
652
|
-
tools: {}
|
|
653
|
-
}
|
|
654
|
-
}
|
|
655
|
-
);
|
|
656
|
-
server.setRequestHandler(ListToolsRequestSchema, async () => ({
|
|
657
|
-
tools: adapters.map(({ tool }) => serializeTool(tool, metadataMap))
|
|
658
|
-
}));
|
|
659
|
-
server.setRequestHandler(CallToolRequestSchema, async (request) => {
|
|
660
|
-
const entry = adapters.find(({ tool }) => {
|
|
661
|
-
const toolName = tool.metadata?.name || tool.filename;
|
|
662
|
-
return toolName === request.params.name;
|
|
663
|
-
});
|
|
664
|
-
if (!entry) {
|
|
665
|
-
throw new Error(`Tool ${request.params.name} not found or not MCP-enabled`);
|
|
666
|
-
}
|
|
667
|
-
try {
|
|
668
|
-
return await entry.invoke(request.params.arguments);
|
|
669
|
-
} catch (error) {
|
|
670
|
-
const message = error && error.message || String(error);
|
|
671
|
-
return {
|
|
672
|
-
content: [{ type: "text", text: `Error: ${message}` }],
|
|
673
|
-
isError: true
|
|
674
|
-
};
|
|
675
|
-
}
|
|
676
|
-
});
|
|
677
|
-
const transport = new StdioServerTransport();
|
|
678
|
-
await server.connect(transport);
|
|
679
|
-
console.error("MCP stdio server started");
|
|
680
|
-
}
|
|
681
|
-
function buildAdapters(tools) {
|
|
682
|
-
return tools.filter((tool) => isMcpEnabled(tool)).map((tool) => {
|
|
683
|
-
const httpHandlers = toHttpHandlerMap(tool.httpHandlers);
|
|
684
|
-
const adapterOptions = {
|
|
685
|
-
name: tool.metadata?.name || tool.filename,
|
|
686
|
-
httpHandlers,
|
|
687
|
-
...tool.schema ? { schema: tool.schema } : {},
|
|
688
|
-
...tool.mcpConfig?.defaultMethod ? { defaultMethod: tool.mcpConfig.defaultMethod } : {}
|
|
689
|
-
};
|
|
690
|
-
const adapter = createMcpAdapter(adapterOptions);
|
|
691
|
-
return {
|
|
692
|
-
tool,
|
|
693
|
-
invoke: adapter
|
|
694
|
-
};
|
|
695
|
-
});
|
|
696
|
-
}
|
|
697
|
-
async function loadToolsFromDirectory(metadataMap) {
|
|
698
|
-
const tools = [];
|
|
699
|
-
const toolsDir = path.join(process.cwd(), "tools");
|
|
700
|
-
if (!fs.existsSync(toolsDir)) {
|
|
701
|
-
return tools;
|
|
702
|
-
}
|
|
703
|
-
const files = fs.readdirSync(toolsDir);
|
|
704
|
-
for (const file of files) {
|
|
705
|
-
if (!isSupportedToolFile(file)) {
|
|
706
|
-
continue;
|
|
707
|
-
}
|
|
708
|
-
const toolPath = path.join(toolsDir, file);
|
|
709
|
-
try {
|
|
710
|
-
const exportsObject = __require(toolPath);
|
|
711
|
-
const candidate = resolveModuleCandidate(exportsObject);
|
|
712
|
-
if (!candidate?.schema) {
|
|
713
|
-
continue;
|
|
714
|
-
}
|
|
715
|
-
const baseName = file.replace(/\.[^.]+$/, "");
|
|
716
|
-
const name = candidate.metadata?.name || baseName;
|
|
717
|
-
const meta = metadataMap.get(name);
|
|
718
|
-
let inputSchema = meta?.inputSchema;
|
|
719
|
-
if (!inputSchema) {
|
|
720
|
-
try {
|
|
721
|
-
inputSchema = zodToJsonSchema(candidate.schema, {
|
|
722
|
-
name: `${name}Schema`,
|
|
723
|
-
target: "jsonSchema7",
|
|
724
|
-
$refStrategy: "none"
|
|
725
|
-
});
|
|
726
|
-
} catch {
|
|
727
|
-
inputSchema = { type: "object" };
|
|
728
|
-
}
|
|
729
|
-
}
|
|
730
|
-
inputSchema = normalizeInputSchema(inputSchema);
|
|
731
|
-
const payment = candidate.payment ?? null;
|
|
732
|
-
const httpHandlersRaw = collectHttpHandlers(candidate);
|
|
733
|
-
const httpHandlers = [...httpHandlersRaw];
|
|
734
|
-
if (httpHandlers.length === 0) {
|
|
735
|
-
continue;
|
|
736
|
-
}
|
|
737
|
-
if (payment) {
|
|
738
|
-
for (let index = 0; index < httpHandlers.length; index += 1) {
|
|
739
|
-
const entry = httpHandlers[index];
|
|
740
|
-
httpHandlers[index] = {
|
|
741
|
-
...entry,
|
|
742
|
-
handler: withX402Payment(entry.handler, payment)
|
|
743
|
-
};
|
|
744
|
-
}
|
|
745
|
-
}
|
|
746
|
-
const mcpConfig = normalizeRuntimeMcpConfig(candidate.mcp);
|
|
747
|
-
const adapterOptions = {
|
|
748
|
-
name,
|
|
749
|
-
httpHandlers: toHttpHandlerMap(httpHandlers),
|
|
750
|
-
...candidate.schema ? { schema: candidate.schema } : {},
|
|
751
|
-
...typeof candidate.mcp?.defaultMethod === "string" ? { defaultMethod: candidate.mcp.defaultMethod } : {}
|
|
752
|
-
};
|
|
753
|
-
const adapter = createMcpAdapter(adapterOptions);
|
|
754
|
-
const tool = {
|
|
755
|
-
...candidate.schema ? { schema: candidate.schema } : {},
|
|
756
|
-
inputSchema,
|
|
757
|
-
metadata: candidate.metadata || meta || null,
|
|
758
|
-
filename: baseName,
|
|
759
|
-
httpHandlers,
|
|
760
|
-
mcpConfig,
|
|
761
|
-
handler: async (params) => adapter(params),
|
|
762
|
-
payment
|
|
763
|
-
};
|
|
764
|
-
tools.push(tool);
|
|
765
|
-
} catch (error) {
|
|
766
|
-
console.warn(`Failed to load tool from ${file}: ${error}`);
|
|
767
|
-
}
|
|
768
|
-
}
|
|
769
|
-
return tools;
|
|
770
|
-
}
|
|
771
|
-
function loadMetadata() {
|
|
772
|
-
const metadataPath = path.join(process.cwd(), "metadata.json");
|
|
773
|
-
if (!fs.existsSync(metadataPath)) {
|
|
774
|
-
return null;
|
|
775
|
-
}
|
|
776
|
-
try {
|
|
777
|
-
const contents = fs.readFileSync(metadataPath, "utf8");
|
|
778
|
-
return JSON.parse(contents);
|
|
779
|
-
} catch (error) {
|
|
780
|
-
console.warn(`Failed to parse metadata.json: ${error}`);
|
|
781
|
-
return null;
|
|
782
|
-
}
|
|
783
|
-
}
|
|
784
|
-
function buildMetadataMap(metadata) {
|
|
785
|
-
const map = /* @__PURE__ */ new Map();
|
|
786
|
-
if (!metadata?.tools) {
|
|
787
|
-
return map;
|
|
788
|
-
}
|
|
789
|
-
metadata.tools.forEach((tool) => {
|
|
790
|
-
map.set(tool.name, tool);
|
|
791
|
-
});
|
|
792
|
-
return map;
|
|
793
|
-
}
|
|
794
|
-
function serializeTool(tool, metadataMap) {
|
|
795
|
-
const name = tool.metadata?.name || tool.filename;
|
|
796
|
-
const meta = metadataMap.get(name);
|
|
797
|
-
return {
|
|
798
|
-
name,
|
|
799
|
-
description: meta?.description || tool.metadata?.description || `${tool.filename} tool`,
|
|
800
|
-
inputSchema: meta?.inputSchema || tool.inputSchema,
|
|
801
|
-
annotations: meta?.annotations || tool.metadata?.annotations,
|
|
802
|
-
payment: meta?.payment || tool.metadata?.payment,
|
|
803
|
-
discovery: meta?.discovery || tool.metadata?.discovery
|
|
804
|
-
};
|
|
805
|
-
}
|
|
806
|
-
function isSupportedToolFile(file) {
|
|
807
|
-
return /\.(cjs|mjs|js|ts)$/i.test(file);
|
|
808
|
-
}
|
|
809
|
-
function resolveModuleCandidate(exportsObject) {
|
|
810
|
-
if (!exportsObject) {
|
|
811
|
-
return null;
|
|
812
|
-
}
|
|
813
|
-
if (exportsObject.schema) {
|
|
814
|
-
return exportsObject;
|
|
815
|
-
}
|
|
816
|
-
if (exportsObject.default && exportsObject.default.schema) {
|
|
817
|
-
return exportsObject.default;
|
|
818
|
-
}
|
|
819
|
-
return exportsObject;
|
|
820
|
-
}
|
|
821
|
-
function collectHttpHandlers(module) {
|
|
822
|
-
const handlers = [];
|
|
823
|
-
HTTP_METHODS.forEach((method) => {
|
|
824
|
-
const handler = module?.[method];
|
|
825
|
-
if (typeof handler === "function") {
|
|
826
|
-
handlers.push({
|
|
827
|
-
method,
|
|
828
|
-
handler: async (request) => handler.call(module, request)
|
|
829
|
-
});
|
|
830
|
-
}
|
|
831
|
-
});
|
|
832
|
-
return handlers;
|
|
833
|
-
}
|
|
834
|
-
function toHttpHandlerMap(handlers) {
|
|
835
|
-
return handlers.reduce((acc, handler) => {
|
|
836
|
-
acc[handler.method.toUpperCase()] = handler.handler;
|
|
837
|
-
return acc;
|
|
838
|
-
}, {});
|
|
839
|
-
}
|
|
840
|
-
function normalizeInputSchema(schema) {
|
|
841
|
-
if (!schema || typeof schema !== "object") {
|
|
842
|
-
return schema;
|
|
843
|
-
}
|
|
844
|
-
const clone = JSON.parse(JSON.stringify(schema));
|
|
845
|
-
if (typeof clone.$ref === "string" && clone.$ref.startsWith("#/definitions/")) {
|
|
846
|
-
const refKey = clone.$ref.replace("#/definitions/", "");
|
|
847
|
-
if (clone.definitions && typeof clone.definitions[refKey] === "object") {
|
|
848
|
-
return normalizeInputSchema(clone.definitions[refKey]);
|
|
849
|
-
}
|
|
850
|
-
}
|
|
851
|
-
delete clone.$ref;
|
|
852
|
-
delete clone.definitions;
|
|
853
|
-
if (!clone.type) {
|
|
854
|
-
clone.type = "object";
|
|
855
|
-
}
|
|
856
|
-
return clone;
|
|
857
|
-
}
|
|
858
|
-
function normalizeRuntimeMcpConfig(rawConfig) {
|
|
859
|
-
if (isPlainObject(rawConfig) && rawConfig.enabled === true) {
|
|
860
|
-
let normalizedMode;
|
|
861
|
-
if (typeof rawConfig.mode === "string") {
|
|
862
|
-
const candidate = rawConfig.mode.toLowerCase();
|
|
863
|
-
if (candidate === "stdio" || candidate === "lambda" || candidate === "dual") {
|
|
864
|
-
normalizedMode = candidate;
|
|
865
|
-
} else {
|
|
866
|
-
throw new Error('mcp.mode must be one of "stdio", "lambda", or "dual"');
|
|
867
|
-
}
|
|
868
|
-
}
|
|
869
|
-
const metadataOverrides = isPlainObject(rawConfig.metadataOverrides) ? rawConfig.metadataOverrides : void 0;
|
|
870
|
-
const config = { enabled: true };
|
|
871
|
-
if (normalizedMode) {
|
|
872
|
-
config.mode = normalizedMode;
|
|
873
|
-
}
|
|
874
|
-
if (typeof rawConfig.defaultMethod === "string") {
|
|
875
|
-
config.defaultMethod = rawConfig.defaultMethod.toUpperCase();
|
|
876
|
-
}
|
|
877
|
-
if (metadataOverrides) {
|
|
878
|
-
config.metadataOverrides = metadataOverrides;
|
|
879
|
-
}
|
|
880
|
-
return config;
|
|
881
|
-
}
|
|
882
|
-
return null;
|
|
883
|
-
}
|
|
884
|
-
function isPlainObject(value) {
|
|
885
|
-
return typeof value === "object" && value !== null && !Array.isArray(value);
|
|
886
|
-
}
|
|
887
|
-
function isMcpEnabled(tool) {
|
|
888
|
-
return Boolean(tool.mcpConfig?.enabled);
|
|
889
|
-
}
|
|
890
|
-
function resolveRuntimePath(value) {
|
|
891
|
-
if (value.startsWith("file://")) {
|
|
892
|
-
return fileURLToPath(value);
|
|
893
|
-
}
|
|
894
|
-
return path.resolve(value);
|
|
895
|
-
}
|
|
896
|
-
|
|
897
|
-
// src/types/index.ts
|
|
898
|
-
var HTTP_METHODS2 = ["GET", "HEAD", "POST", "PUT", "DELETE", "PATCH", "OPTIONS"];
|
|
899
475
|
var X402Client = class {
|
|
900
476
|
constructor(config) {
|
|
901
477
|
this.account = privateKeyToAccount(config.privateKey);
|
|
@@ -1721,8 +1297,8 @@ async function store(input, options) {
|
|
|
1721
1297
|
);
|
|
1722
1298
|
}
|
|
1723
1299
|
const { baseUrl, apiKey, fetchFn } = resolveConfig(options);
|
|
1724
|
-
const
|
|
1725
|
-
const url = `${baseUrl}${
|
|
1300
|
+
const path = mode === "backtest" ? "/apps/backtests/tx" : "/apps/positions/tx";
|
|
1301
|
+
const url = `${baseUrl}${path}`;
|
|
1726
1302
|
let response;
|
|
1727
1303
|
try {
|
|
1728
1304
|
response = await fetchFn(url, {
|
|
@@ -1762,8 +1338,8 @@ async function store(input, options) {
|
|
|
1762
1338
|
async function retrieve(params, options) {
|
|
1763
1339
|
const { baseUrl, apiKey, fetchFn } = resolveConfig(options);
|
|
1764
1340
|
const mode = params?.mode ?? "live";
|
|
1765
|
-
const
|
|
1766
|
-
const url = new URL(`${baseUrl}${
|
|
1341
|
+
const path = mode === "backtest" ? "/apps/backtests/tx" : "/apps/positions/tx";
|
|
1342
|
+
const url = new URL(`${baseUrl}${path}`);
|
|
1767
1343
|
if (params?.source) url.searchParams.set("source", params.source);
|
|
1768
1344
|
if (params?.walletAddress) url.searchParams.set("walletAddress", params.walletAddress);
|
|
1769
1345
|
if (params?.symbol) url.searchParams.set("symbol", params.symbol);
|
|
@@ -5881,9 +5457,9 @@ function parseOptionalDate(value) {
|
|
|
5881
5457
|
function buildHmacSignature(args) {
|
|
5882
5458
|
const timestamp = args.timestamp.toString();
|
|
5883
5459
|
const method = args.method.toUpperCase();
|
|
5884
|
-
const
|
|
5460
|
+
const path = args.path;
|
|
5885
5461
|
const body = args.body == null ? "" : typeof args.body === "string" ? args.body : JSON.stringify(args.body);
|
|
5886
|
-
const payload = `${timestamp}${method}${
|
|
5462
|
+
const payload = `${timestamp}${method}${path}${body}`;
|
|
5887
5463
|
const key = Buffer.from(args.secret, "base64");
|
|
5888
5464
|
return createHmac("sha256", key).update(payload).digest("hex");
|
|
5889
5465
|
}
|
|
@@ -7675,8 +7251,8 @@ async function streamText(options, clientConfig = {}) {
|
|
|
7675
7251
|
const handlers = options.handlers ?? {};
|
|
7676
7252
|
let finishedResolve;
|
|
7677
7253
|
let finishedReject;
|
|
7678
|
-
const finished = new Promise((
|
|
7679
|
-
finishedResolve =
|
|
7254
|
+
const finished = new Promise((resolve, reject) => {
|
|
7255
|
+
finishedResolve = resolve;
|
|
7680
7256
|
finishedReject = reject;
|
|
7681
7257
|
});
|
|
7682
7258
|
let settled = false;
|
|
@@ -7905,9 +7481,9 @@ function assignIfDefined(target, key, value) {
|
|
|
7905
7481
|
target[key] = value;
|
|
7906
7482
|
}
|
|
7907
7483
|
}
|
|
7908
|
-
function buildUrl(baseUrl,
|
|
7484
|
+
function buildUrl(baseUrl, path) {
|
|
7909
7485
|
const sanitizedBase = baseUrl.endsWith("/") ? baseUrl.slice(0, -1) : baseUrl;
|
|
7910
|
-
return `${sanitizedBase}${
|
|
7486
|
+
return `${sanitizedBase}${path}`;
|
|
7911
7487
|
}
|
|
7912
7488
|
function createAbortBundle(upstreamSignal, timeoutMs) {
|
|
7913
7489
|
const controller = new AbortController();
|
|
@@ -8111,7 +7687,118 @@ function buildBacktestDecisionSeriesInput(request) {
|
|
|
8111
7687
|
...accountValueUsd != null ? { accountValueUsd } : {}
|
|
8112
7688
|
};
|
|
8113
7689
|
}
|
|
7690
|
+
function createMcpAdapter(options) {
|
|
7691
|
+
const normalizedSchema = ensureSchema(options.schema);
|
|
7692
|
+
const defaultMethod = resolveDefaultMethod(options);
|
|
7693
|
+
const httpHandler = options.httpHandlers[defaultMethod];
|
|
7694
|
+
if (!httpHandler) {
|
|
7695
|
+
throw new Error(`Tool "${options.name}" does not export an HTTP handler for ${defaultMethod}`);
|
|
7696
|
+
}
|
|
7697
|
+
return async function invoke(rawArguments) {
|
|
7698
|
+
const validated = normalizedSchema ? normalizedSchema.parse(rawArguments ?? {}) : rawArguments;
|
|
7699
|
+
const request = buildRequest(options.name, defaultMethod, validated);
|
|
7700
|
+
try {
|
|
7701
|
+
const response = await Promise.resolve(httpHandler(request));
|
|
7702
|
+
return await responseToToolResponse(response);
|
|
7703
|
+
} catch (error) {
|
|
7704
|
+
if (error instanceof X402PaymentRequiredError) {
|
|
7705
|
+
return await responseToToolResponse(error.response);
|
|
7706
|
+
}
|
|
7707
|
+
throw error;
|
|
7708
|
+
}
|
|
7709
|
+
};
|
|
7710
|
+
}
|
|
7711
|
+
function resolveDefaultMethod(options) {
|
|
7712
|
+
const explicit = options.defaultMethod?.toUpperCase();
|
|
7713
|
+
if (explicit && typeof options.httpHandlers[explicit] === "function") {
|
|
7714
|
+
return explicit;
|
|
7715
|
+
}
|
|
7716
|
+
const preferredOrder = ["POST", "PUT", "PATCH", "GET", "DELETE", "OPTIONS", "HEAD"];
|
|
7717
|
+
for (const method of preferredOrder) {
|
|
7718
|
+
if (typeof options.httpHandlers[method] === "function") {
|
|
7719
|
+
return method;
|
|
7720
|
+
}
|
|
7721
|
+
}
|
|
7722
|
+
const available = Object.keys(options.httpHandlers).filter(
|
|
7723
|
+
(method) => typeof options.httpHandlers[method] === "function"
|
|
7724
|
+
);
|
|
7725
|
+
if (available.length > 0) {
|
|
7726
|
+
return available[0];
|
|
7727
|
+
}
|
|
7728
|
+
throw new Error(`No HTTP handlers available for tool "${options.name}"`);
|
|
7729
|
+
}
|
|
7730
|
+
function ensureSchema(schema) {
|
|
7731
|
+
if (!schema) {
|
|
7732
|
+
return void 0;
|
|
7733
|
+
}
|
|
7734
|
+
if (schema instanceof z.ZodType) {
|
|
7735
|
+
return schema;
|
|
7736
|
+
}
|
|
7737
|
+
if (typeof schema?.parse === "function") {
|
|
7738
|
+
return schema;
|
|
7739
|
+
}
|
|
7740
|
+
throw new Error("MCP adapter requires a valid Zod schema to validate arguments");
|
|
7741
|
+
}
|
|
7742
|
+
function buildRequest(name, method, params) {
|
|
7743
|
+
const url = new URL(`https://opentool.local/${encodeURIComponent(name)}`);
|
|
7744
|
+
const headers = new Headers({
|
|
7745
|
+
"x-opentool-invocation": "mcp",
|
|
7746
|
+
"x-opentool-tool": name
|
|
7747
|
+
});
|
|
7748
|
+
if (method === "GET" || method === "HEAD") {
|
|
7749
|
+
if (params && typeof params === "object") {
|
|
7750
|
+
Object.entries(params).forEach(([key, value]) => {
|
|
7751
|
+
if (value == null) {
|
|
7752
|
+
return;
|
|
7753
|
+
}
|
|
7754
|
+
url.searchParams.set(key, String(value));
|
|
7755
|
+
});
|
|
7756
|
+
}
|
|
7757
|
+
return new Request(url, { method, headers });
|
|
7758
|
+
}
|
|
7759
|
+
headers.set("Content-Type", "application/json");
|
|
7760
|
+
const init = { method, headers };
|
|
7761
|
+
if (params != null) {
|
|
7762
|
+
init.body = JSON.stringify(params);
|
|
7763
|
+
}
|
|
7764
|
+
return new Request(url, init);
|
|
7765
|
+
}
|
|
7766
|
+
async function responseToToolResponse(response) {
|
|
7767
|
+
const statusIsError = response.status >= 400;
|
|
7768
|
+
const contentType = response.headers.get("content-type") ?? "";
|
|
7769
|
+
const text = await response.text();
|
|
7770
|
+
if (contentType.includes("application/json")) {
|
|
7771
|
+
try {
|
|
7772
|
+
const payload = text ? JSON.parse(text) : {};
|
|
7773
|
+
if (payload && typeof payload === "object" && Array.isArray(payload.content)) {
|
|
7774
|
+
return {
|
|
7775
|
+
content: payload.content,
|
|
7776
|
+
isError: payload.isError ?? statusIsError
|
|
7777
|
+
};
|
|
7778
|
+
}
|
|
7779
|
+
return {
|
|
7780
|
+
content: [{ type: "text", text: JSON.stringify(payload, null, 2) }],
|
|
7781
|
+
isError: statusIsError
|
|
7782
|
+
};
|
|
7783
|
+
} catch {
|
|
7784
|
+
return {
|
|
7785
|
+
content: [{ type: "text", text }],
|
|
7786
|
+
isError: statusIsError
|
|
7787
|
+
};
|
|
7788
|
+
}
|
|
7789
|
+
}
|
|
7790
|
+
if (!text) {
|
|
7791
|
+
return {
|
|
7792
|
+
content: [],
|
|
7793
|
+
isError: statusIsError
|
|
7794
|
+
};
|
|
7795
|
+
}
|
|
7796
|
+
return {
|
|
7797
|
+
content: [{ type: "text", text }],
|
|
7798
|
+
isError: statusIsError
|
|
7799
|
+
};
|
|
7800
|
+
}
|
|
8114
7801
|
|
|
8115
|
-
export { AIAbortError, AIError, AIFetchError, AIResponseError, BACKTEST_DECISION_MODE, DEFAULT_BASE_URL, DEFAULT_CHAIN, DEFAULT_FACILITATOR, DEFAULT_HYPERLIQUID_CADENCE_CRON, DEFAULT_HYPERLIQUID_MARKET_SLIPPAGE_BPS, DEFAULT_HYPERLIQUID_TPSL_MARKET_SLIPPAGE_BPS, DEFAULT_MODEL, DEFAULT_OPENPOND_GATEWAY_URL2 as DEFAULT_OPENPOND_GATEWAY_URL, DEFAULT_TIMEOUT_MS, DEFAULT_TOKENS,
|
|
7802
|
+
export { AIAbortError, AIError, AIFetchError, AIResponseError, BACKTEST_DECISION_MODE, DEFAULT_BASE_URL, DEFAULT_CHAIN, DEFAULT_FACILITATOR, DEFAULT_HYPERLIQUID_CADENCE_CRON, DEFAULT_HYPERLIQUID_MARKET_SLIPPAGE_BPS, DEFAULT_HYPERLIQUID_TPSL_MARKET_SLIPPAGE_BPS, DEFAULT_MODEL, DEFAULT_OPENPOND_GATEWAY_URL2 as DEFAULT_OPENPOND_GATEWAY_URL, DEFAULT_TIMEOUT_MS, DEFAULT_TOKENS, HTTP_METHODS, HYPERLIQUID_HIP3_DEXES, HyperliquidApiError, HyperliquidBuilderApprovalError, HyperliquidExchangeClient, HyperliquidGuardError, HyperliquidInfoClient, HyperliquidTermsError, NewsSignalClient, PAYMENT_HEADERS, POLYMARKET_CHAIN_ID, POLYMARKET_CLOB_AUTH_DOMAIN, POLYMARKET_CLOB_DOMAIN, POLYMARKET_ENDPOINTS, POLYMARKET_EXCHANGE_ADDRESSES, PolymarketApiError, PolymarketAuthError, PolymarketExchangeClient, PolymarketInfoClient, SUPPORTED_CURRENCIES, StoreError, WEBSEARCH_TOOL_DEFINITION, WEBSEARCH_TOOL_NAME, X402BrowserClient, X402Client, X402PaymentRequiredError, __hyperliquidInternals, __hyperliquidMarketDataInternals, approveHyperliquidBuilderFee, backtestDecisionRequestSchema, batchModifyHyperliquidOrders, buildBacktestDecisionSeriesInput, buildHmacSignature, buildHyperliquidMarketDescriptor, buildHyperliquidMarketIdentity, buildHyperliquidProfileAssets, buildHyperliquidSpotUsdPriceMap, buildL1Headers, buildL2Headers, buildPolymarketApprovalTransactions, buildPolymarketOrderAmounts, buildPolymarketOutcomeTokenApprovalTransactions, buildPolymarketUsdcApprovalTransaction, buildSignedOrderPayload, cancelAllHyperliquidOrders, cancelAllPolymarketOrders, cancelHyperliquidOrders, cancelHyperliquidOrdersByCloid, cancelHyperliquidTwapOrder, cancelMarketPolymarketOrders, cancelPolymarketOrder, cancelPolymarketOrders, chains, clampHyperliquidAbs, clampHyperliquidFloat, clampHyperliquidInt, computeHyperliquidMarketIocLimitPrice, createAIClient, createHyperliquidSubAccount, createMcpAdapter, createMonotonicNonceFactory, createOrDerivePolymarketApiKey, createPolymarketApiKey, decodePolymarketBootstrapTransaction, defineX402Payment, depositToHyperliquidBridge, derivePolymarketApiKey, ensureTextContent, estimateCountBack, estimateHyperliquidLiquidationPrice, evaluateNewsContinuationGate, executeTool, extractHyperliquidDex, extractHyperliquidOrderIds, fetchHyperliquidActiveAsset, fetchHyperliquidAllMids, fetchHyperliquidAssetCtxs, fetchHyperliquidBars, fetchHyperliquidClearinghouseState, fetchHyperliquidDexMeta, fetchHyperliquidDexMetaAndAssetCtxs, fetchHyperliquidFrontendOpenOrders, fetchHyperliquidFrontendOpenOrdersAcrossDexes, fetchHyperliquidHistoricalOrders, fetchHyperliquidMeta, fetchHyperliquidMetaAndAssetCtxs, fetchHyperliquidOpenOrders, fetchHyperliquidOpenOrdersAcrossDexes, fetchHyperliquidOrderStatus, fetchHyperliquidPerpMarketInfo, fetchHyperliquidPreTransferCheck, fetchHyperliquidResolvedMarketDescriptor, fetchHyperliquidSizeDecimals, fetchHyperliquidSpotAccountValue, fetchHyperliquidSpotAssetCtxs, fetchHyperliquidSpotClearinghouseState, fetchHyperliquidSpotMarketInfo, fetchHyperliquidSpotMeta, fetchHyperliquidSpotMetaAndAssetCtxs, fetchHyperliquidSpotTickSize, fetchHyperliquidSpotUsdPriceMap, fetchHyperliquidTickSize, fetchHyperliquidUserFills, fetchHyperliquidUserFillsByTime, fetchHyperliquidUserRateLimit, fetchNewsEventSignal, fetchNewsPropositionSignal, fetchPolymarketActivity, fetchPolymarketApprovalState, fetchPolymarketClosedPositions, fetchPolymarketDepositAddresses, fetchPolymarketMarket, fetchPolymarketMarkets, fetchPolymarketMidpoint, fetchPolymarketOrderbook, fetchPolymarketPositionValue, fetchPolymarketPositions, fetchPolymarketPrice, fetchPolymarketPriceHistory, fetchPolymarketPublicProfile, flattenMessageContent, formatHyperliquidMarketablePrice, formatHyperliquidOrderSize, formatHyperliquidPrice, formatHyperliquidSize, generateText, getHyperliquidMaxBuilderFee, getKnownHyperliquidDexes, getModelConfig, getMyPerformance, getMyTools, getRpcUrl, getX402PaymentContext, isHyperliquidSpotSymbol, isStreamingSupported, isToolCallingSupported, listModels, modifyHyperliquidOrder, normalizeHyperliquidBaseSymbol, normalizeHyperliquidDcaEntries, normalizeHyperliquidIndicatorBars, normalizeHyperliquidMetaSymbol, normalizeModelName, normalizeNumberArrayish, normalizeSpotTokenName2 as normalizeSpotTokenName, normalizeStringArrayish, parseHyperliquidJson, parseHyperliquidSymbol, parseSpotPairSymbol, parseTimeToSeconds, payX402, payX402WithWallet, placeHyperliquidOrder2 as placeHyperliquidOrder, placeHyperliquidOrderWithTpSl, placeHyperliquidPositionTpSl, placeHyperliquidTwapOrder, placePolymarketOrder, planHyperliquidTrade, postAgentDigest, readHyperliquidAccountValue, readHyperliquidNumber, readHyperliquidPerpPosition, readHyperliquidPerpPositionSize, readHyperliquidSpotAccountValue, readHyperliquidSpotBalance, readHyperliquidSpotBalanceSize, recordHyperliquidBuilderApproval, recordHyperliquidTermsAcceptance, registry, requireX402Payment, reserveHyperliquidRequestWeight, resolutionToSeconds, resolveBacktestAccountValueUsd, resolveBacktestMode, resolveBacktestWindow, resolveConfig2 as resolveConfig, resolveExchangeAddress, resolveHyperliquidAbstractionFromMode, resolveHyperliquidBudgetUsd, resolveHyperliquidCadenceCron, resolveHyperliquidCadenceFromResolution, resolveHyperliquidChain, resolveHyperliquidChainConfig, resolveHyperliquidDcaSymbolEntries, resolveHyperliquidErrorDetail, resolveHyperliquidHourlyInterval, resolveHyperliquidIntervalCron, resolveHyperliquidLeverageMode, resolveHyperliquidMarketDataCoin, resolveHyperliquidMaxPerRunUsd, resolveHyperliquidOrderRef, resolveHyperliquidOrderSymbol, resolveHyperliquidPair, resolveHyperliquidPerpSymbol, resolveHyperliquidProfileChain, resolveHyperliquidRpcEnvVar, resolveHyperliquidScheduleEvery, resolveHyperliquidScheduleUnit, resolveHyperliquidSpotSymbol, resolveHyperliquidStoreNetwork, resolveHyperliquidSymbol, resolveHyperliquidTargetSize, resolveNewsGatewayBase, resolvePolymarketBaseUrl, resolvePolymarketBootstrapContracts, resolveSpotMidCandidates, resolveSpotTokenCandidates, resolveToolset, responseToToolResponse, retrieve, roundHyperliquidPriceToTick, scheduleHyperliquidCancel, sendHyperliquidSpot, setHyperliquidAccountAbstractionMode, setHyperliquidPortfolioMargin, store, streamText, supportsHyperliquidBuilderFee, tokens, transferHyperliquidSubAccount, updateHyperliquidIsolatedMargin, updateHyperliquidLeverage, wallet, walletToolkit, withX402Payment, withdrawFromHyperliquid };
|
|
8116
7803
|
//# sourceMappingURL=index.js.map
|
|
8117
7804
|
//# sourceMappingURL=index.js.map
|