Skip to content

Commit b265398

Browse files
author
oleina
committed
review round 1: use path.join, fix mismatched url
1 parent 5034ab7 commit b265398

File tree

2 files changed

+22
-14
lines changed

2 files changed

+22
-14
lines changed

src/mcp/tools/database/get_data.ts

Lines changed: 11 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ import { mcpError, toContent } from "../../util";
44
import * as url from "node:url";
55
import { Client } from "../../../apiv2";
66
import { text } from "node:stream/consumers";
7+
import path from "node:path";
78

89
export const get_data = tool(
910
{
@@ -14,7 +15,7 @@ export const get_data = tool(
1415
.string()
1516
.optional()
1617
.describe(
17-
"connect to the database at url. If omitted, use default database instance <project>-default-rtdb.firebaseio.com. Can point to emulator URL (e.g. localhost:6000/<instance>)",
18+
"connect to the database at url. If omitted, use default database instance <project>-default-rtdb.firebasedatabase.app. Can point to emulator URL (e.g. localhost:6000/<instance>)",
1819
),
1920
path: z.string().describe("The path to the data to read. (ex: /my/cool/path)"),
2021
}),
@@ -32,23 +33,26 @@ export const get_data = tool(
3233
requiresProject: false,
3334
},
3435
},
35-
async ({ path, databaseUrl }, { projectId, host }) => {
36-
if (!path.startsWith("/")) {
37-
return mcpError(`paths must start with '/' (you passed ''${path}')`);
36+
async ({ path: getPath, databaseUrl }, { projectId, host }) => {
37+
if (!getPath.startsWith("/")) {
38+
return mcpError(`paths must start with '/' (you passed ''${getPath}')`);
3839
}
3940

4041
const dbUrl = new url.URL(
4142
databaseUrl
42-
? `${databaseUrl}/${path}.json`
43-
: `https://${projectId}-default-rtdb.us-central1.firebasedatabase.app/${path}.json`,
43+
? `${databaseUrl}/${getPath}.json`
44+
: path.join(
45+
`https://${projectId}-default-rtdb.us-central1.firebasedatabase.app`,
46+
`${getPath}.json`,
47+
),
4448
);
4549

4650
const client = new Client({
4751
urlPrefix: dbUrl.origin,
4852
auth: true,
4953
});
5054

51-
host.logger.debug(`sending read request to path '${path}' for url '${dbUrl.toString()}'`);
55+
host.logger.debug(`sending read request to path '${getPath}' for url '${dbUrl.toString()}'`);
5256

5357
const res = await client.request<unknown, NodeJS.ReadableStream>({
5458
method: "GET",

src/mcp/tools/database/set_data.ts

Lines changed: 11 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ import * as url from "node:url";
55
import { stringToStream } from "../../../utils";
66
import { Client } from "../../../apiv2";
77
import { getErrMsg } from "../../../error";
8+
import path from "node:path";
89

910
export const set_data = tool(
1011
{
@@ -15,7 +16,7 @@ export const set_data = tool(
1516
.string()
1617
.optional()
1718
.describe(
18-
"connect to the database at url. If omitted, use default database instance <project>-default-rtdb.firebaseio.com. Can point to emulator URL (e.g. localhost:6000/<instance>)",
19+
"connect to the database at url. If omitted, use default database instance <project>-default-rtdb.us-central1.firebasedatabase.app. Can point to emulator URL (e.g. localhost:6000/<instance>)",
1920
),
2021
path: z.string().describe("The path to the data to read. (ex: /my/cool/path)"),
2122
data: z.string().describe('The JSON to write. (ex: {"alphabet": ["a", "b", "c"]})'),
@@ -31,15 +32,18 @@ export const set_data = tool(
3132
requiresProject: false,
3233
},
3334
},
34-
async ({ path, databaseUrl, data }, { projectId, host }) => {
35-
if (!path.startsWith("/")) {
36-
return mcpError(`paths must start with '/' (you passed ''${path}')`);
35+
async ({ path: setPath, databaseUrl, data }, { projectId, host }) => {
36+
if (!setPath.startsWith("/")) {
37+
return mcpError(`paths must start with '/' (you passed ''${setPath}')`);
3738
}
3839

3940
const dbUrl = new url.URL(
4041
databaseUrl
41-
? `${databaseUrl}/${path}.json`
42-
: `https://${projectId}-default-rtdb.us-central1.firebasedatabase.app/${path}.json`,
42+
? `${databaseUrl}/${setPath}.json`
43+
: path.join(
44+
`https://${projectId}-default-rtdb.us-central1.firebasedatabase.app`,
45+
`${setPath}.json`,
46+
),
4347
);
4448

4549
const client = new Client({
@@ -49,7 +53,7 @@ export const set_data = tool(
4953

5054
const inStream = stringToStream(data);
5155

52-
host.logger.debug(`sending write request to path '${path}' for url '${dbUrl.toString()}'`);
56+
host.logger.debug(`sending write request to path '${setPath}' for url '${dbUrl.toString()}'`);
5357

5458
try {
5559
await client.request({

0 commit comments

Comments
 (0)