-
Notifications
You must be signed in to change notification settings - Fork 181
feat: add CLI functionality to the MCP server #602
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Changes from 4 commits
Commits
Show all changes
8 commits
Select commit
Hold shift + click to select a range
7696a3e
initial work
fmhall 83fe68b
removing unused exports
fmhall cb76494
cli context for verbose logging
fmhall b108190
fix
fmhall 8c54dc0
make server default
fmhall f4da924
Merge branch 'main' into mason/cli
fmhall 560edd8
fixes
fmhall 2131454
fix checks
fmhall File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,25 @@ | ||
| { | ||
| "mode": "pre", | ||
| "tag": "beta", | ||
| "initialVersions": { | ||
| "@x402scan/proxy": "1.0.0", | ||
| "@x402scan/solana-rpc": "0.0.0", | ||
| "@x402scan/app": "0.1.0", | ||
| "facilitators": "0.0.10", | ||
| "@x402scan/mcp": "0.2.1", | ||
| "@x402scan/siwx": "0.0.1", | ||
| "@x402scan/eslint-config": "0.0.0", | ||
| "@x402scan/typescript-config": "0.0.0", | ||
| "@x402scan/analytics-db": "1.0.0", | ||
| "@x402scan/partners-db": "1.0.0", | ||
| "@x402scan/scan-db": "1.0.0", | ||
| "prisma-client-1d8bcb17ac472d49b060ef3f01db4b30365f35bcce0282b975003196210c16f5": "6.19.0", | ||
| "@x402scan/transfers-db": "1.0.0", | ||
| "prisma-client-bfff2ef213b3a2f8bed96e55a92d5644e66afcab25ce177991a5c8039418c607": "6.19.0", | ||
| "@x402scan/neverthrow": "1.0.0", | ||
| "@x402scan/sync-alerts": "1.0.0", | ||
| "@x402scan/sync-analytics": "1.0.0", | ||
| "@x402scan/sync-transfers": "1.0.0" | ||
| }, | ||
| "changesets": [] | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,112 @@ | ||
| import { randomBytes } from 'crypto'; | ||
|
|
||
| import { | ||
| successResponse, | ||
| errorResponse, | ||
| fromNeverthrowError, | ||
| outputAndExit, | ||
| type OutputFlags, | ||
| } from '@/cli/output'; | ||
|
|
||
| import { buildRequest } from '@/server/tools/lib/request'; | ||
| import { checkEndpoint } from '@/shared/operations'; | ||
| import { safeParseResponse } from '@/shared/neverthrow/fetch'; | ||
| import { getWalletOrExit, parseRequestInput } from './lib'; | ||
|
|
||
| import type { GlobalFlags } from '@/types'; | ||
| import type { JsonObject } from '@/shared/neverthrow/json/types'; | ||
|
|
||
| const SURFACE = 'cli:check'; | ||
|
|
||
| interface CheckArgs { | ||
| url: string; | ||
| method?: string; | ||
| body?: string; | ||
| headers?: string; | ||
| } | ||
|
|
||
| export async function checkCommand( | ||
| args: CheckArgs, | ||
| flags: GlobalFlags<OutputFlags> | ||
| ): Promise<void> { | ||
| const { account } = await getWalletOrExit(flags); | ||
| const sessionId = randomBytes(16).toString('hex'); | ||
|
|
||
| const input = parseRequestInput(SURFACE, args, flags); | ||
|
|
||
| const request = buildRequest({ | ||
| input, | ||
| address: account.address, | ||
| sessionId, | ||
| }); | ||
|
|
||
| const result = await checkEndpoint(SURFACE, request); | ||
|
|
||
| if (result.isErr()) { | ||
| outputAndExit(fromNeverthrowError(result), flags); | ||
| } | ||
|
|
||
| const value = result.value; | ||
|
|
||
| // Handle Response (non-ok HTTP response) | ||
| if (value instanceof Response) { | ||
| const response = value; | ||
| const parseResult = await safeParseResponse(SURFACE, response); | ||
| const details: JsonObject = { statusCode: response.status }; | ||
| if (parseResult.isOk()) { | ||
| const { type } = parseResult.value; | ||
| if (type === 'json') { | ||
| details.body = parseResult.value.data; | ||
| } else if (type === 'text') { | ||
| details.body = parseResult.value.data; | ||
| } else { | ||
| details.bodyType = type; | ||
| } | ||
| } | ||
| outputAndExit( | ||
| errorResponse({ | ||
| code: 'HTTP_ERROR', | ||
| message: `HTTP ${response.status}: ${response.statusText}`, | ||
| surface: SURFACE, | ||
| cause: 'http', | ||
| details, | ||
| }), | ||
| flags | ||
| ); | ||
| } | ||
|
|
||
| // Handle CheckEndpointPaidResult | ||
| if ('requiresPayment' in value && value.requiresPayment) { | ||
| outputAndExit( | ||
| successResponse({ | ||
| requiresPayment: true, | ||
| statusCode: value.statusCode, | ||
| routeDetails: value.routeDetails, | ||
| }), | ||
| flags | ||
| ); | ||
| } | ||
|
|
||
| // Handle CheckEndpointFreeResult | ||
| if ('data' in value) { | ||
| outputAndExit( | ||
| successResponse({ | ||
| requiresPayment: false, | ||
| statusCode: value.statusCode, | ||
| data: value.data, | ||
| }), | ||
| flags | ||
| ); | ||
| } | ||
|
|
||
| // Fallback - shouldn't reach here | ||
| outputAndExit( | ||
| errorResponse({ | ||
| code: 'GENERAL_ERROR', | ||
| message: 'Unexpected response format', | ||
| surface: SURFACE, | ||
| cause: 'unknown', | ||
| }), | ||
| flags | ||
| ); | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,46 @@ | ||
| import { | ||
| successResponse, | ||
| errorResponse, | ||
| outputAndExit, | ||
| type OutputFlags, | ||
| } from '@/cli/output'; | ||
|
|
||
| import { discoverResources } from '@/shared/operations'; | ||
|
|
||
| import type { GlobalFlags } from '@/types'; | ||
|
|
||
| interface DiscoverArgs { | ||
| url: string; | ||
| } | ||
|
|
||
| export async function discoverCommand( | ||
| args: DiscoverArgs, | ||
| flags: GlobalFlags<OutputFlags> | ||
| ): Promise<void> { | ||
| const result = await discoverResources('cli:discover', args.url); | ||
|
|
||
| if (result.isOk()) { | ||
| outputAndExit( | ||
| successResponse({ | ||
| found: true, | ||
| origin: result.value.origin, | ||
| source: result.value.source, | ||
| ...(result.value.usage ? { usage: result.value.usage } : {}), | ||
| data: result.value.data, | ||
| }), | ||
| flags | ||
| ); | ||
| } | ||
|
|
||
| // Error case | ||
| outputAndExit( | ||
| errorResponse({ | ||
| code: 'GENERAL_ERROR', | ||
| message: result.error.message, | ||
| surface: result.error.surface, | ||
| cause: result.error.cause, | ||
| details: { origin: result.error.origin }, | ||
| }), | ||
| flags | ||
| ); | ||
| } | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,133 @@ | ||
| import { randomBytes } from 'crypto'; | ||
|
|
||
| import { x402Client, x402HTTPClient } from '@x402/core/client'; | ||
| import { ExactEvmScheme } from '@x402/evm/exact/client'; | ||
|
|
||
| import { | ||
| successResponse, | ||
| errorResponse, | ||
| fromNeverthrowError, | ||
| outputAndExit, | ||
| type OutputFlags, | ||
| } from '@/cli/output'; | ||
|
|
||
| import { buildRequest } from '@/server/tools/lib/request'; | ||
| import { DEFAULT_NETWORK } from '@/shared/networks'; | ||
| import { tokenStringToNumber } from '@/shared/token'; | ||
| import { safeParseResponse } from '@/shared/neverthrow/fetch'; | ||
| import { safeGetPaymentSettlement } from '@/shared/neverthrow/x402'; | ||
| import { createFetchWithPayment } from '@/shared/operations'; | ||
| import { getWalletOrExit, parseRequestInput } from './lib'; | ||
|
|
||
| import type { GlobalFlags } from '@/types'; | ||
| import type { JsonObject } from '@/shared/neverthrow/json/types'; | ||
|
|
||
| const SURFACE = 'cli:fetch'; | ||
|
|
||
| interface FetchArgs { | ||
| url: string; | ||
| method?: string; | ||
| body?: string; | ||
| headers?: string; | ||
| } | ||
|
|
||
| export async function fetchCommand( | ||
| args: FetchArgs, | ||
| flags: GlobalFlags<OutputFlags> | ||
| ): Promise<void> { | ||
| const { account } = await getWalletOrExit(flags); | ||
| const sessionId = randomBytes(16).toString('hex'); | ||
|
|
||
| const input = parseRequestInput(SURFACE, args, flags); | ||
|
|
||
| // Set up x402 client | ||
| const coreClient = x402Client.fromConfig({ | ||
| schemes: [ | ||
| { network: DEFAULT_NETWORK, client: new ExactEvmScheme(account) }, | ||
| ], | ||
| }); | ||
|
|
||
| const client = new x402HTTPClient(coreClient); | ||
|
|
||
| const request = buildRequest({ | ||
| input, | ||
| address: account.address, | ||
| sessionId, | ||
| }); | ||
|
|
||
| const fetchWithPay = createFetchWithPayment(SURFACE, client); | ||
| const fetchResult = await fetchWithPay(request); | ||
|
|
||
| if (fetchResult.isErr()) { | ||
| outputAndExit(fromNeverthrowError(fetchResult), flags); | ||
| } | ||
|
|
||
| const { response, paymentPayload } = fetchResult.value; | ||
|
|
||
| if (!response.ok) { | ||
| const parseResult = await safeParseResponse(SURFACE, response); | ||
| const details: JsonObject = { statusCode: response.status }; | ||
| if (parseResult.isOk()) { | ||
| const { type } = parseResult.value; | ||
| if (type === 'json') { | ||
| details.body = parseResult.value.data; | ||
| } else if (type === 'text') { | ||
| details.body = parseResult.value.data; | ||
| } else { | ||
| details.bodyType = type; | ||
| } | ||
| } | ||
| outputAndExit( | ||
| errorResponse({ | ||
| code: 'HTTP_ERROR', | ||
| message: `HTTP ${response.status}: ${response.statusText}`, | ||
| surface: SURFACE, | ||
| cause: 'http', | ||
| details, | ||
| }), | ||
| flags | ||
| ); | ||
| } | ||
|
|
||
| const parseResponseResult = await safeParseResponse(SURFACE, response); | ||
| if (parseResponseResult.isErr()) { | ||
| outputAndExit(fromNeverthrowError(parseResponseResult), flags); | ||
| } | ||
|
|
||
| const settlementResult = safeGetPaymentSettlement(SURFACE, client, response); | ||
|
|
||
| // Build response data | ||
| const data = | ||
| parseResponseResult.value.type === 'json' | ||
| ? parseResponseResult.value.data | ||
| : parseResponseResult.value.type === 'text' | ||
| ? parseResponseResult.value.data | ||
| : { type: parseResponseResult.value.type }; | ||
|
|
||
| // Build metadata | ||
| const metadata = | ||
| settlementResult.isOk() || paymentPayload !== undefined | ||
| ? { | ||
| ...(paymentPayload !== undefined | ||
| ? { | ||
| price: tokenStringToNumber( | ||
| paymentPayload.accepted.amount | ||
| ).toLocaleString('en-US', { | ||
| style: 'currency', | ||
| currency: 'USD', | ||
| }), | ||
| } | ||
| : {}), | ||
| ...(settlementResult.isOk() | ||
| ? { | ||
| payment: { | ||
| success: settlementResult.value.success, | ||
| transactionHash: settlementResult.value.transaction, | ||
| }, | ||
| } | ||
| : {}), | ||
| } | ||
| : undefined; | ||
|
|
||
| outputAndExit(successResponse(data, metadata), flags); | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,6 @@ | ||
| export { fetchCommand } from './fetch'; | ||
| export { checkCommand } from './check'; | ||
| export { discoverCommand } from './discover'; | ||
| export { walletInfoCommand, walletRedeemCommand } from './wallet'; | ||
| export { reportErrorCommand } from './report-error'; | ||
| export { serverCommand } from './server'; |
30 changes: 30 additions & 0 deletions
30
packages/external/mcp/src/cli/commands/lib/get-wallet-or-exit.ts
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,30 @@ | ||
| import { | ||
| fromNeverthrowError, | ||
| outputAndExit, | ||
| type OutputFlags, | ||
| } from '@/cli/output'; | ||
| import { getWallet } from '@/shared/wallet'; | ||
|
|
||
| import type { GlobalFlags } from '@/types'; | ||
| import type { PrivateKeyAccount } from 'viem/accounts'; | ||
|
|
||
| interface WalletInfo { | ||
| account: PrivateKeyAccount; | ||
| } | ||
|
|
||
| /** | ||
| * Get wallet or exit with error. | ||
| * This function always returns a valid wallet - if getting the wallet fails, | ||
| * it exits the process with an appropriate error. | ||
| */ | ||
| export async function getWalletOrExit( | ||
| flags: GlobalFlags<OutputFlags> | ||
| ): Promise<WalletInfo> { | ||
| const walletResult = await getWallet(); | ||
|
|
||
| if (walletResult.isErr()) { | ||
| outputAndExit(fromNeverthrowError(walletResult, 'WALLET_ERROR'), flags); | ||
| } | ||
|
|
||
| return walletResult.value; | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,2 @@ | ||
| export { getWalletOrExit } from './get-wallet-or-exit'; | ||
| export { parseRequestInput } from './parse-request-input'; |
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.