Skip to content

Commit 05c325b

Browse files
committed
🐛 fix get_latest_block tool
1 parent 4c3a31d commit 05c325b

File tree

2 files changed

+18
-11
lines changed

2 files changed

+18
-11
lines changed

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "bitcoin-mcp",
3-
"version": "0.0.4",
3+
"version": "0.0.5",
44
"description": "A Model Context Protocol server for interacting with Bitcoin.",
55
"type": "module",
66
"main": "./build/index.js",

src/services/bitcoin.ts

Lines changed: 17 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -80,7 +80,7 @@ export class BitcoinService {
8080
logger.error({ error }, "Failed to generate key");
8181
throw new BitcoinError(
8282
"Failed to generate key pair",
83-
BitcoinErrorCode.KEY_GENERATION_ERROR,
83+
BitcoinErrorCode.KEY_GENERATION_ERROR
8484
);
8585
}
8686
}
@@ -140,7 +140,7 @@ export class BitcoinService {
140140
logger.error({ error, rawHex }, "Failed to decode transaction");
141141
throw new BitcoinError(
142142
"Failed to decode transaction",
143-
BitcoinErrorCode.DECODE_ERROR,
143+
BitcoinErrorCode.DECODE_ERROR
144144
);
145145
}
146146
}
@@ -155,11 +155,18 @@ export class BitcoinService {
155155
*/
156156
async getLatestBlock(): Promise<BlockInfo> {
157157
try {
158-
const response = await fetch(`${this.apiBase}/blocks/tip`);
159-
if (!response.ok) {
160-
throw new Error(`HTTP error! status: ${response.status}`);
158+
const hashRes = await fetch(`${this.apiBase}/blocks/tip/hash`);
159+
if (!hashRes.ok) {
160+
throw new Error("Failed to fetch latest block hash");
161161
}
162-
const block = (await response.json()) as any;
162+
const hash = await hashRes.text();
163+
164+
const blockRes = await fetch(`${this.apiBase}/block/${hash}`);
165+
if (!blockRes.ok) {
166+
throw new Error("Failed to fetch block data");
167+
}
168+
const block = (await blockRes.json()) as BlockstreamBlock;
169+
163170
return {
164171
hash: block.id,
165172
height: block.height,
@@ -169,10 +176,10 @@ export class BitcoinService {
169176
weight: block.weight,
170177
};
171178
} catch (error) {
172-
logger.error({ error }, "Failed to get latest block");
179+
logger.error({ error }, "Failed to fetch latest block");
173180
throw new BitcoinError(
174-
"Failed to get latest block",
175-
BitcoinErrorCode.BLOCKCHAIN_ERROR,
181+
"Failed to fetch latest block",
182+
BitcoinErrorCode.BLOCKCHAIN_ERROR
176183
);
177184
}
178185
}
@@ -227,7 +234,7 @@ export class BitcoinService {
227234
logger.error({ error, txid }, "Failed to get transaction");
228235
throw new BitcoinError(
229236
"Failed to get transaction",
230-
BitcoinErrorCode.BLOCKCHAIN_ERROR,
237+
BitcoinErrorCode.BLOCKCHAIN_ERROR
231238
);
232239
}
233240
}

0 commit comments

Comments
 (0)