Repository navigation
Fixed feat(mcp): register get_contract_status tool #234
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
Merged
Changes from 2 commits
Commits
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
Large diffs are not rendered by default.
Oops, something went wrong.
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
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,95 @@ | ||
| import type Database from "better-sqlite3"; | ||
| import { getContract, getEntriesForContract } from "../db/repositories.js"; | ||
| import type { ContractEntry } from "../db/repositories.js"; | ||
| import { | ||
| classifyTTL, | ||
| formatTimeToCloseLedger, | ||
| type TTLStatus, | ||
| } from "../utils/formatting.js"; | ||
|
|
||
| export type EntryTTLStatus = TTLStatus | "unknown"; | ||
|
|
||
| export type ContractStatusEntry = { | ||
| label: string; | ||
| entryType: string; | ||
| entryKeyXdr: string; | ||
| liveUntilLedger: number | null; | ||
| remainingTTL: number | null; | ||
| approximateTimeRemaining: string | null; | ||
| status: EntryTTLStatus; | ||
| }; | ||
|
|
||
| export type ContractStatus = { | ||
| contractId: string; | ||
| name: string | null; | ||
| network: string; | ||
| lastCheckedLedger: number | null; | ||
| entries: ContractStatusEntry[]; | ||
| }; | ||
|
|
||
| export class ContractNotFoundError extends Error { | ||
| constructor(contractId: string) { | ||
| super(`Contract ${contractId} is not registered.`); | ||
| this.name = "ContractNotFoundError"; | ||
| } | ||
| } | ||
|
|
||
| function getEntryLabel(entry: ContractEntry): string { | ||
| if (entry.entry_type === "instance") return "Instance"; | ||
| if (entry.entry_type === "wasm") return "WASM Code"; | ||
| return entry.label ?? entry.entry_type; | ||
| } | ||
|
|
||
| function mapEntryStatus( | ||
| entry: ContractEntry, | ||
| lastCheckedLedger: number | null, | ||
| ): ContractStatusEntry { | ||
| const label = getEntryLabel(entry); | ||
| const liveUntilLedger = entry.live_until_ledger ?? null; | ||
|
|
||
| if (liveUntilLedger == null || lastCheckedLedger == null) { | ||
| return { | ||
| label, | ||
| entryType: entry.entry_type, | ||
| entryKeyXdr: entry.entry_key_xdr, | ||
| liveUntilLedger, | ||
| remainingTTL: null, | ||
| approximateTimeRemaining: null, | ||
| status: "unknown", | ||
| }; | ||
| } | ||
|
|
||
| const remainingTTL = liveUntilLedger - lastCheckedLedger; | ||
| const status = classifyTTL(remainingTTL); | ||
|
|
||
| return { | ||
| label, | ||
| entryType: entry.entry_type, | ||
| entryKeyXdr: entry.entry_key_xdr, | ||
| liveUntilLedger, | ||
| remainingTTL, | ||
| approximateTimeRemaining: formatTimeToCloseLedger(remainingTTL), | ||
| status, | ||
| }; | ||
| } | ||
|
|
||
| export function getContractStatus(db: Database.Database, contractId: string): ContractStatus { | ||
| const contract = getContract(db, contractId); | ||
|
|
||
| if (!contract) { | ||
| throw new ContractNotFoundError(contractId); | ||
| } | ||
|
|
||
| const lastCheckedLedger = contract.last_checked_ledger ?? null; | ||
| const entries = getEntriesForContract(db, contractId).map((entry) => | ||
| mapEntryStatus(entry, lastCheckedLedger), | ||
| ); | ||
|
|
||
| return { | ||
| contractId: contract.id, | ||
| name: contract.name, | ||
| network: contract.network, | ||
| lastCheckedLedger, | ||
| entries, | ||
| }; | ||
| } | ||
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,15 @@ | ||
| #!/usr/bin/env node | ||
| import { StdioServerTransport } from "@modelcontextprotocol/sdk/server/stdio.js"; | ||
| import { getDatabase } from "../db/database.js"; | ||
| import { createMcpServer } from "./server.js"; | ||
|
|
||
| async function main(): Promise<void> { | ||
| const server = createMcpServer(() => getDatabase()); | ||
| const transport = new StdioServerTransport(); | ||
| await server.connect(transport); | ||
| } | ||
|
|
||
| main().catch((error: unknown) => { | ||
| console.error("MCP server error:", error); | ||
| process.exit(1); | ||
| }); | ||
|
bigjoe-io marked this conversation as resolved.
Resolved
|
||
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,14 @@ | ||
| import type Database from "better-sqlite3"; | ||
| import { McpServer } from "@modelcontextprotocol/sdk/server/mcp.js"; | ||
| import { registerGetContractStatusTool } from "./tools/get_contract_status.js"; | ||
|
|
||
| export function createMcpServer(getDb: () => Database.Database): McpServer { | ||
| const server = new McpServer({ | ||
| name: "sorokeep", | ||
| version: "0.1.2", | ||
| }); | ||
|
|
||
| registerGetContractStatusTool(server, getDb); | ||
|
|
||
| return server; | ||
| } | ||
|
bigjoe-io marked this conversation as resolved.
Resolved
|
||
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,52 @@ | ||
| import type Database from "better-sqlite3"; | ||
| import type { McpServer } from "@modelcontextprotocol/sdk/server/mcp.js"; | ||
| import type { CallToolResult } from "@modelcontextprotocol/sdk/types.js"; | ||
| import * as z from "zod/v4"; | ||
| import { | ||
| ContractNotFoundError, | ||
| getContractStatus, | ||
| type ContractStatus, | ||
| } from "../../core/status.js"; | ||
|
|
||
| export async function invokeGetContractStatus( | ||
| db: Database.Database, | ||
| contractId: string, | ||
| ): Promise<CallToolResult> { | ||
| try { | ||
| const status = getContractStatus(db, contractId); | ||
| return formatContractStatusResult(status); | ||
| } catch (error) { | ||
| if (error instanceof ContractNotFoundError) { | ||
| return { | ||
| content: [{ type: "text", text: error.message }], | ||
| isError: true, | ||
| }; | ||
| } | ||
| throw error; | ||
| } | ||
| } | ||
|
|
||
| export function formatContractStatusResult(status: ContractStatus): CallToolResult { | ||
| return { | ||
| content: [{ type: "text", text: JSON.stringify(status, null, 2) }], | ||
| structuredContent: { ...status }, | ||
| }; | ||
| } | ||
|
|
||
| export function registerGetContractStatusTool( | ||
| server: McpServer, | ||
| getDb: () => Database.Database, | ||
| ): void { | ||
| server.registerTool( | ||
| "get_contract_status", | ||
| { | ||
| title: "Get Contract Status", | ||
| description: | ||
| "Query TTL health metrics for a watched Soroban contract, including per-entry remaining lifespans.", | ||
| inputSchema: { | ||
| contractId: z.string().describe("Stellar contract address (C... format)"), | ||
| }, | ||
| }, | ||
| async ({ contractId }) => invokeGetContractStatus(getDb(), contractId), | ||
| ); | ||
| } | ||
|
bigjoe-io marked this conversation as resolved.
Resolved
|
||
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.