splitwise-mcp 3.2.3 → 3.3.1

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/dist/client.js CHANGED
@@ -1,6 +1,6 @@
1
1
  import { dirname, join } from 'path';
2
2
  import { fileURLToPath } from 'url';
3
- import { loadDotenvSafely, readEnvVar, createApiClient, ApiError, } from '@chrischall/mcp-utils';
3
+ import { loadDotenvSafely, requireEnvVar, createApiClient, ApiError, } from '@chrischall/mcp-utils';
4
4
  // Load .env for local dev; silently skip if dotenv is unavailable (e.g. mcpb
5
5
  // bundle). `loadDotenvSafely` swallows a missing dotenv module and never lets
6
6
  // .env override a host-provided value.
@@ -17,6 +17,13 @@ catch {
17
17
  const BASE_URL = 'https://secure.splitwise.com/api/v3.0';
18
18
  const SERVICE_NAME = 'Splitwise';
19
19
  const SPLITWISE_DOMAIN = 'splitwise.com';
20
+ /**
21
+ * Client-wide cap on any body read (fleet-audit #733). Generous for the
22
+ * largest JSON listing Splitwise returns, small enough that a broken or
23
+ * hostile upstream cannot exhaust memory. A call can override it with
24
+ * `maxBytes` (the receipt download does).
25
+ */
26
+ export const MAX_RESPONSE_BYTES = 16 * 1024 * 1024;
20
27
  /**
21
28
  * Whether a hostname belongs to Splitwise, and may therefore be sent the API
22
29
  * key. Receipt bytes are served either from a Splitwise host (which requires
@@ -90,6 +97,21 @@ function assertWriteSucceeded(data) {
90
97
  const detail = messages.length > 0 ? messages.join('; ') : 'success: false with no error detail';
91
98
  throw new Error(`Splitwise rejected the request: ${detail}`);
92
99
  }
100
+ /**
101
+ * SPLITWISE_API_KEY is required on the stdio path — no tool works without it —
102
+ * so it is read with requireEnvVar (which applies readEnvVar's blank /
103
+ * `undefined` / `null` / `${...}` placeholder handling). The throw is swallowed
104
+ * here only so the constructor can defer it to the first tool call, keeping the
105
+ * install-time tools/list probe working on a server that has no key yet.
106
+ */
107
+ function envApiKey() {
108
+ try {
109
+ return requireEnvVar('SPLITWISE_API_KEY');
110
+ }
111
+ catch {
112
+ return undefined;
113
+ }
114
+ }
93
115
  export class SplitwiseClient {
94
116
  apiKey;
95
117
  /** Which source supplied the API key — a LABEL, never the value. */
@@ -110,11 +132,11 @@ export class SplitwiseClient {
110
132
  */
111
133
  constructor(opts) {
112
134
  // Injected apiKey (hosted per-user path) takes precedence; otherwise fall
113
- // back to the env var. readEnvVar trims whitespace and treats
135
+ // back to the env var. envApiKey trims whitespace and treats
114
136
  // blank/`undefined`/`null`/`${...}` placeholder values as unset — defends
115
137
  // against MCP hosts that pass .mcp.json env blocks through unexpanded.
116
138
  const injected = opts?.apiKey;
117
- const key = injected ?? readEnvVar('SPLITWISE_API_KEY');
139
+ const key = injected ?? envApiKey();
118
140
  // Which source supplied it, for `sw_healthcheck`. Derived from the SAME
119
141
  // `key` and `injected` bindings the resolution above uses, so the two
120
142
  // cannot disagree. An earlier version re-tested `opts?.apiKey` for
@@ -156,6 +178,7 @@ export class SplitwiseClient {
156
178
  serviceName: service,
157
179
  retry: { count: 1, delayMs: 2000 },
158
180
  timeout: 30_000,
181
+ maxResponseBytes: MAX_RESPONSE_BYTES,
159
182
  ...(authenticated ? { getToken: () => this.requireKey() } : {}),
160
183
  onUnauthorized: () => new Error(authenticated
161
184
  ? 'SPLITWISE_API_KEY is invalid or missing'
@@ -183,8 +206,11 @@ export class SplitwiseClient {
183
206
  *
184
207
  * A cross-origin redirect (API → S3) is safe: per the Fetch standard the
185
208
  * `Authorization` header is dropped when a redirect changes origin.
209
+ *
210
+ * `maxBytes` overrides the client-wide body cap for this download; over it,
211
+ * the read is refused (`ResponseTooLargeError`, `kind: 'too_large'`).
186
212
  */
187
- async fetchAsset(url) {
213
+ async fetchAsset(url, opts = {}) {
188
214
  const target = parseAssetUrl(url);
189
215
  const authenticated = isSplitwiseHost(target.hostname);
190
216
  const cacheKey = `${target.origin}|${authenticated ? 'auth' : 'anon'}`;
@@ -197,7 +223,7 @@ export class SplitwiseClient {
197
223
  // `pathname + search` verbatim rather than the client's `query` option:
198
224
  // a presigned signature covers the exact encoding, so re-encoding it
199
225
  // would invalidate the URL.
200
- return await api.fetchRaw('GET', `${target.pathname}${target.search}`);
226
+ return await api.fetchRaw('GET', `${target.pathname}${target.search}`, opts.maxBytes !== undefined ? { maxBytes: opts.maxBytes } : {});
201
227
  }
202
228
  catch (err) {
203
229
  throw redactAssetQuery(err, target.search);
package/dist/index.js CHANGED
@@ -19,7 +19,7 @@ import { registerUtilityTools } from './tools/utilities.js';
19
19
  // client into the same registrars instead of this singleton.
20
20
  await runMcp({
21
21
  name: 'splitwise-mcp',
22
- version: '3.2.3', // x-release-please-version
22
+ version: '3.3.1', // x-release-please-version
23
23
  deps: client,
24
24
  banner: '[splitwise-mcp] This project was developed and is maintained by AI (Claude Sonnet 4.6). Use at your own discretion.',
25
25
  tools: [
@@ -26,6 +26,10 @@ export function confirmWrite(ctx, gate) {
26
26
  details: preview,
27
27
  tool: gate.tool,
28
28
  confirmToken: gate.confirmToken,
29
+ // One API key per SplitwiseClient (the hosted path builds a client per
30
+ // user), so there is no account switch inside a server to bind.
31
+ account: undefined,
32
+ args: gate.args,
29
33
  subject: () => ({ target: String(gate.target), payload: request, preview }),
30
34
  }));
31
35
  }
@@ -0,0 +1,16 @@
1
+ import { UNTRUSTED_CONTENT_RULE, UNTRUSTED_DESCRIPTION_SUFFIX, untrustedEnvelope, untrustedResult, } from '@chrischall/mcp-utils';
2
+ /**
3
+ * Comments, notifications, expense descriptions/details and receipt text are
4
+ * written by OTHER Splitwise users — a roommate, a co-parent, anyone in a
5
+ * shared group — and this server also exposes writes (fleet-audit #737).
6
+ */
7
+ export const SW_UNTRUSTED_NOTE = `Expense descriptions and notes, comments, notifications and receipt text below are written by other Splitwise users (friends and group members), not the user. ${UNTRUSTED_CONTENT_RULE}`;
8
+ export { UNTRUSTED_DESCRIPTION_SUFFIX };
9
+ /** A tool result fencing third-party text: markers first, then the payload. */
10
+ export function swUntrustedResult(payload) {
11
+ return untrustedResult(payload, { note: SW_UNTRUSTED_NOTE });
12
+ }
13
+ /** The same envelope as a plain object, for a result built by hand. */
14
+ export function swUntrustedEnvelope(payload) {
15
+ return untrustedEnvelope(payload, { note: SW_UNTRUSTED_NOTE });
16
+ }
@@ -2,6 +2,7 @@ import { z } from 'zod';
2
2
  import { SW_VIEWS, viewExpense, viewExpenses } from '../project.js';
3
3
  import { buildQueryString, minifiedResult, resolveView, viewParam } from '@chrischall/mcp-utils';
4
4
  import { CONFIRM_NOTE, confirmTokenParam, confirmWrite } from './_confirm.js';
5
+ import { UNTRUSTED_DESCRIPTION_SUFFIX, swUntrustedResult } from './_untrusted.js';
5
6
  /** Flattens a users array into Splitwise's flat-param JSON format. */
6
7
  export function flattenUsers(users) {
7
8
  const flat = {};
@@ -34,8 +35,8 @@ const userShareSchema = z.object({
34
35
  });
35
36
  export function registerExpenseTools(server, client) {
36
37
  server.registerTool('sw_list_expenses', {
37
- description: 'List or search Splitwise expenses. All filters are optional. Use group_id to filter by group, dated_after/dated_before for date ranges.',
38
- annotations: { readOnlyHint: true },
38
+ description: `List or search Splitwise expenses. All filters are optional. Use group_id to filter by group, dated_after/dated_before for date ranges. ${UNTRUSTED_DESCRIPTION_SUFFIX}`,
39
+ annotations: { readOnlyHint: true, openWorldHint: true },
39
40
  inputSchema: z.object({
40
41
  view: viewParam(SW_VIEWS, {
41
42
  note: 'compact keeps the share breakdown, repayments and receipt presence and drops the avatars and the repeat/reminder/transaction block; "full" returns Splitwise\'s whole records.',
@@ -52,8 +53,16 @@ export function registerExpenseTools(server, client) {
52
53
  .optional(),
53
54
  updated_after: z.string().describe('ISO 8601 datetime').optional(),
54
55
  updated_before: z.string().describe('ISO 8601 datetime').optional(),
55
- limit: z.number().describe('Max results (API default: 20)').optional(),
56
- offset: z.number().describe('Pagination offset').optional(),
56
+ // Bounded: Splitwise reads limit=0 as "return everything", an
57
+ // unbounded result the host refuses.
58
+ limit: z
59
+ .number()
60
+ .int()
61
+ .min(1)
62
+ .max(200)
63
+ .describe('Max results, 1-200 (API default: 20). Page with offset for more.')
64
+ .optional(),
65
+ offset: z.number().int().min(0).describe('Pagination offset').optional(),
57
66
  }),
58
67
  }, async (args) => {
59
68
  const qs = buildQueryString({
@@ -67,11 +76,11 @@ export function registerExpenseTools(server, client) {
67
76
  offset: args.offset,
68
77
  });
69
78
  const data = await client.request('GET', `/get_expenses${qs}`);
70
- return minifiedResult(viewExpenses(resolveView(args.view, SW_VIEWS), data));
79
+ return swUntrustedResult(viewExpenses(resolveView(args.view, SW_VIEWS), data));
71
80
  });
72
81
  server.registerTool('sw_get_expense', {
73
- description: 'Get full details of a single Splitwise expense by id.',
74
- annotations: { readOnlyHint: true },
82
+ description: `Get full details of a single Splitwise expense by id. ${UNTRUSTED_DESCRIPTION_SUFFIX}`,
83
+ annotations: { readOnlyHint: true, openWorldHint: true },
75
84
  inputSchema: z.object({
76
85
  view: viewParam(SW_VIEWS, {
77
86
  note: 'compact keeps the share breakdown, repayments and receipt presence and drops the avatars and the repeat/reminder/transaction block; "full" returns Splitwise\'s whole records.',
@@ -80,10 +89,11 @@ export function registerExpenseTools(server, client) {
80
89
  }),
81
90
  }, async ({ id, view }) => {
82
91
  const data = await client.request('GET', `/get_expense/${id}`);
83
- return minifiedResult(viewExpense(resolveView(view, SW_VIEWS), data));
92
+ return swUntrustedResult(viewExpense(resolveView(view, SW_VIEWS), data));
84
93
  });
85
94
  server.registerTool('sw_create_expense', {
86
- description: `Create a Splitwise expense. Use split_equally:true to split evenly among group members, or provide a users array for custom per-person splits (paid_share and owed_share as decimal strings like "25.00"). cost must be a decimal string. ${CONFIRM_NOTE}`,
95
+ description: `Create a Splitwise expense. Use split_equally:true to split evenly among group members, or provide a users array for custom per-person splits (paid_share and owed_share as decimal strings like "25.00"). cost must be a decimal string. This puts charges on every participant's balance and notifies them. ${CONFIRM_NOTE}`,
96
+ annotations: { readOnlyHint: false, destructiveHint: true, openWorldHint: true },
87
97
  inputSchema: z.object({
88
98
  group_id: z.number().describe('Group to add expense to (use 0 for no group)'),
89
99
  description: z.string().describe('Short description of the expense'),
@@ -116,6 +126,7 @@ export function registerExpenseTools(server, client) {
116
126
  body,
117
127
  target: args.group_id,
118
128
  confirmToken: args.confirmToken,
129
+ args,
119
130
  });
120
131
  if (gate)
121
132
  return gate;
@@ -124,6 +135,7 @@ export function registerExpenseTools(server, client) {
124
135
  });
125
136
  server.registerTool('sw_update_expense', {
126
137
  description: `Edit an existing Splitwise expense. Provide expense_id and any fields to change. For custom split updates, the full users array must be provided (the API replaces the entire split). ${CONFIRM_NOTE}`,
138
+ annotations: { readOnlyHint: false, destructiveHint: true, openWorldHint: true },
127
139
  inputSchema: z.object({
128
140
  expense_id: z.number().describe('ID of the expense to update'),
129
141
  description: z.string().optional(),
@@ -151,6 +163,7 @@ export function registerExpenseTools(server, client) {
151
163
  body,
152
164
  target: expense_id,
153
165
  confirmToken: args.confirmToken,
166
+ args,
154
167
  });
155
168
  if (gate)
156
169
  return gate;
@@ -159,12 +172,13 @@ export function registerExpenseTools(server, client) {
159
172
  });
160
173
  server.registerTool('sw_delete_expense', {
161
174
  description: `Soft-delete a Splitwise expense by id. Returns {success: true} on success. Use sw_undelete_expense to restore. ${CONFIRM_NOTE}`,
162
- annotations: { destructiveHint: true },
175
+ annotations: { readOnlyHint: false, destructiveHint: true, openWorldHint: true },
163
176
  inputSchema: z.object({
164
177
  id: z.number().describe('Expense ID to delete'),
165
178
  confirmToken: confirmTokenParam,
166
179
  }),
167
- }, async ({ id, confirmToken }, ctx) => {
180
+ }, async (args, ctx) => {
181
+ const { id, confirmToken } = args;
168
182
  const gate = await confirmWrite(ctx, {
169
183
  tool: 'sw_delete_expense',
170
184
  action: 'expense.delete',
@@ -173,6 +187,7 @@ export function registerExpenseTools(server, client) {
173
187
  path: `/delete_expense/${id}`,
174
188
  target: id,
175
189
  confirmToken,
190
+ args,
176
191
  });
177
192
  if (gate)
178
193
  return gate;
@@ -180,11 +195,28 @@ export function registerExpenseTools(server, client) {
180
195
  return minifiedResult(data);
181
196
  });
182
197
  server.registerTool('sw_undelete_expense', {
183
- description: 'Restore a soft-deleted Splitwise expense.',
198
+ description: `Restore a soft-deleted Splitwise expense. This puts its charges back on every participant's balance and notifies them, so it is gated like the other writes. ${CONFIRM_NOTE}`,
199
+ // Destructive despite the delete tool being its record-level inverse:
200
+ // the restore notifies every participant, and no later call un-sends that.
201
+ annotations: { readOnlyHint: false, destructiveHint: true, openWorldHint: true },
184
202
  inputSchema: z.object({
185
203
  id: z.number().describe('Expense ID to restore'),
204
+ confirmToken: confirmTokenParam,
186
205
  }),
187
- }, async ({ id }) => {
206
+ }, async (args, ctx) => {
207
+ const { id, confirmToken } = args;
208
+ const gate = await confirmWrite(ctx, {
209
+ tool: 'sw_undelete_expense',
210
+ action: 'expense.undelete',
211
+ summary: `Restore soft-deleted Splitwise expense ${id} — puts it back on balances and notifies its participants`,
212
+ method: 'POST',
213
+ path: `/undelete_expense/${id}`,
214
+ target: id,
215
+ confirmToken,
216
+ args,
217
+ });
218
+ if (gate)
219
+ return gate;
188
220
  const data = await client.request('POST', `/undelete_expense/${id}`);
189
221
  return minifiedResult(data);
190
222
  });
@@ -5,7 +5,7 @@ import { CONFIRM_NOTE, confirmTokenParam, confirmWrite } from './_confirm.js';
5
5
  export function registerFriendTools(server, client) {
6
6
  server.registerTool('sw_list_friends', {
7
7
  description: "List all Splitwise friends. Use this to resolve a friend's name to a user_id before adding them to a group or building a custom expense split. The compact default returns id, name (first_name + last_name joined), email, registration_status and any non-empty balance per friend; pass view:'full' for Splitwise's raw records, which keep first_name and last_name separate.",
8
- annotations: { readOnlyHint: true },
8
+ annotations: { readOnlyHint: true, openWorldHint: true },
9
9
  inputSchema: z.object({
10
10
  view: viewParam(SW_VIEWS, { note: PERSON_VIEW_NOTE }),
11
11
  }),
@@ -15,14 +15,15 @@ export function registerFriendTools(server, client) {
15
15
  });
16
16
  server.registerTool('sw_create_friend', {
17
17
  description: `Add a Splitwise friend by email (sends them an invite). ${CONFIRM_NOTE}`,
18
- annotations: { destructiveHint: true },
18
+ annotations: { readOnlyHint: false, destructiveHint: true, openWorldHint: true },
19
19
  inputSchema: z.object({
20
20
  user_email: z.string().describe('Email of the user to add as a friend'),
21
21
  user_first_name: z.string().describe('First name of the user').optional(),
22
22
  user_last_name: z.string().describe('Last name of the user').optional(),
23
23
  confirmToken: confirmTokenParam,
24
24
  }),
25
- }, async ({ user_email, user_first_name, user_last_name, confirmToken }, ctx) => {
25
+ }, async (args, ctx) => {
26
+ const { user_email, user_first_name, user_last_name, confirmToken } = args;
26
27
  const body = pruneUndefined({ user_email, user_first_name, user_last_name });
27
28
  const gate = await confirmWrite(ctx, {
28
29
  tool: 'sw_create_friend',
@@ -33,6 +34,7 @@ export function registerFriendTools(server, client) {
33
34
  body,
34
35
  target: user_email,
35
36
  confirmToken,
37
+ args,
36
38
  });
37
39
  if (gate)
38
40
  return gate;
@@ -41,12 +43,13 @@ export function registerFriendTools(server, client) {
41
43
  });
42
44
  server.registerTool('sw_delete_friend', {
43
45
  description: `Remove a Splitwise friendship by user id. ${CONFIRM_NOTE}`,
44
- annotations: { destructiveHint: true },
46
+ annotations: { readOnlyHint: false, destructiveHint: true, openWorldHint: true },
45
47
  inputSchema: z.object({
46
48
  id: z.number().describe('User ID of the friend to remove'),
47
49
  confirmToken: confirmTokenParam,
48
50
  }),
49
- }, async ({ id, confirmToken }, ctx) => {
51
+ }, async (args, ctx) => {
52
+ const { id, confirmToken } = args;
50
53
  const gate = await confirmWrite(ctx, {
51
54
  tool: 'sw_delete_friend',
52
55
  action: 'friend.delete',
@@ -55,6 +58,7 @@ export function registerFriendTools(server, client) {
55
58
  path: `/delete_friend/${id}`,
56
59
  target: id,
57
60
  confirmToken,
61
+ args,
58
62
  });
59
63
  if (gate)
60
64
  return gate;
@@ -5,7 +5,7 @@ import { CONFIRM_NOTE, confirmTokenParam, confirmWrite } from './_confirm.js';
5
5
  export function registerGroupTools(server, client) {
6
6
  server.registerTool('sw_list_groups', {
7
7
  description: 'List all Splitwise groups the current user belongs to. Returns id, name, and members for each group. Use this to resolve a group name to its id.',
8
- annotations: { readOnlyHint: true },
8
+ annotations: { readOnlyHint: true, openWorldHint: true },
9
9
  inputSchema: z.object({
10
10
  view: viewParam(SW_VIEWS, {
11
11
  note: 'compact drops the avatar/cover-photo URLs (60% of a live 51-group response, which does not fit in a tool result at all) and the whiteboard/reminder settings; "full" returns Splitwise\'s whole records.',
@@ -17,7 +17,7 @@ export function registerGroupTools(server, client) {
17
17
  });
18
18
  server.registerTool('sw_get_group', {
19
19
  description: 'Get details of a single Splitwise group including all members and balances.',
20
- annotations: { readOnlyHint: true },
20
+ annotations: { readOnlyHint: true, openWorldHint: true },
21
21
  inputSchema: z.object({
22
22
  view: viewParam(SW_VIEWS, {
23
23
  note: 'compact drops the avatar/cover-photo URLs (60% of a live 51-group response, which does not fit in a tool result at all) and the whiteboard/reminder settings; "full" returns Splitwise\'s whole records.',
@@ -30,7 +30,7 @@ export function registerGroupTools(server, client) {
30
30
  });
31
31
  server.registerTool('sw_create_group', {
32
32
  description: `Create a new Splitwise group. ${CONFIRM_NOTE}`,
33
- annotations: { destructiveHint: true },
33
+ annotations: { readOnlyHint: false, destructiveHint: false, openWorldHint: true },
34
34
  inputSchema: z.object({
35
35
  name: z.string().describe('Group name'),
36
36
  group_type: z
@@ -43,7 +43,8 @@ export function registerGroupTools(server, client) {
43
43
  .optional(),
44
44
  confirmToken: confirmTokenParam,
45
45
  }),
46
- }, async ({ name, group_type, simplify_by_default, confirmToken }, ctx) => {
46
+ }, async (args, ctx) => {
47
+ const { name, group_type, simplify_by_default, confirmToken } = args;
47
48
  const body = pruneUndefined({ name, group_type, simplify_by_default });
48
49
  const gate = await confirmWrite(ctx, {
49
50
  tool: 'sw_create_group',
@@ -54,6 +55,7 @@ export function registerGroupTools(server, client) {
54
55
  body,
55
56
  target: '',
56
57
  confirmToken,
58
+ args,
57
59
  });
58
60
  if (gate)
59
61
  return gate;
@@ -62,6 +64,7 @@ export function registerGroupTools(server, client) {
62
64
  });
63
65
  server.registerTool('sw_add_user_to_group', {
64
66
  description: `Add a user to a Splitwise group. Provide user_id (preferred, use sw_list_friends to resolve a name) or first_name + last_name + email to invite by email. ${CONFIRM_NOTE}`,
67
+ annotations: { readOnlyHint: false, destructiveHint: true, openWorldHint: true },
65
68
  inputSchema: z.object({
66
69
  group_id: z.number().describe('Group ID'),
67
70
  user_id: z.number().describe('User ID (preferred)').optional(),
@@ -70,7 +73,8 @@ export function registerGroupTools(server, client) {
70
73
  email: z.string().optional(),
71
74
  confirmToken: confirmTokenParam,
72
75
  }),
73
- }, async ({ group_id, user_id, first_name, last_name, email, confirmToken }, ctx) => {
76
+ }, async (args, ctx) => {
77
+ const { group_id, user_id, first_name, last_name, email, confirmToken } = args;
74
78
  let body;
75
79
  if (user_id !== undefined) {
76
80
  body = { group_id, user_id };
@@ -90,6 +94,7 @@ export function registerGroupTools(server, client) {
90
94
  body,
91
95
  target: group_id,
92
96
  confirmToken,
97
+ args,
93
98
  });
94
99
  if (gate)
95
100
  return gate;
@@ -98,13 +103,14 @@ export function registerGroupTools(server, client) {
98
103
  });
99
104
  server.registerTool('sw_remove_user_from_group', {
100
105
  description: `Remove a user from a Splitwise group. ${CONFIRM_NOTE}`,
101
- annotations: { destructiveHint: true },
106
+ annotations: { readOnlyHint: false, destructiveHint: true, openWorldHint: true },
102
107
  inputSchema: z.object({
103
108
  group_id: z.number().describe('Group ID'),
104
109
  user_id: z.number().describe('User ID to remove'),
105
110
  confirmToken: confirmTokenParam,
106
111
  }),
107
- }, async ({ group_id, user_id, confirmToken }, ctx) => {
112
+ }, async (args, ctx) => {
113
+ const { group_id, user_id, confirmToken } = args;
108
114
  const gate = await confirmWrite(ctx, {
109
115
  tool: 'sw_remove_user_from_group',
110
116
  action: 'group.remove_user',
@@ -114,6 +120,7 @@ export function registerGroupTools(server, client) {
114
120
  body: { group_id, user_id },
115
121
  target: group_id,
116
122
  confirmToken,
123
+ args,
117
124
  });
118
125
  if (gate)
119
126
  return gate;
@@ -122,12 +129,13 @@ export function registerGroupTools(server, client) {
122
129
  });
123
130
  server.registerTool('sw_delete_group', {
124
131
  description: `Soft-delete a Splitwise group. ${CONFIRM_NOTE}`,
125
- annotations: { destructiveHint: true },
132
+ annotations: { readOnlyHint: false, destructiveHint: true, openWorldHint: true },
126
133
  inputSchema: z.object({
127
134
  id: z.number().describe('Group ID to delete'),
128
135
  confirmToken: confirmTokenParam,
129
136
  }),
130
- }, async ({ id, confirmToken }, ctx) => {
137
+ }, async (args, ctx) => {
138
+ const { id, confirmToken } = args;
131
139
  const gate = await confirmWrite(ctx, {
132
140
  tool: 'sw_delete_group',
133
141
  action: 'group.delete',
@@ -136,6 +144,7 @@ export function registerGroupTools(server, client) {
136
144
  path: `/delete_group/${id}`,
137
145
  target: id,
138
146
  confirmToken,
147
+ args,
139
148
  });
140
149
  if (gate)
141
150
  return gate;
@@ -143,11 +152,28 @@ export function registerGroupTools(server, client) {
143
152
  return minifiedResult(data);
144
153
  });
145
154
  server.registerTool('sw_undelete_group', {
146
- description: 'Restore a soft-deleted Splitwise group.',
155
+ description: `Restore a soft-deleted Splitwise group. This puts its charges back on every participant's balance and notifies them, so it is gated like the other writes. ${CONFIRM_NOTE}`,
156
+ // Destructive despite the delete tool being its record-level inverse:
157
+ // the restore notifies every participant, and no later call un-sends that.
158
+ annotations: { readOnlyHint: false, destructiveHint: true, openWorldHint: true },
147
159
  inputSchema: z.object({
148
160
  id: z.number().describe('Group ID to restore'),
161
+ confirmToken: confirmTokenParam,
149
162
  }),
150
- }, async ({ id }) => {
163
+ }, async (args, ctx) => {
164
+ const { id, confirmToken } = args;
165
+ const gate = await confirmWrite(ctx, {
166
+ tool: 'sw_undelete_group',
167
+ action: 'group.undelete',
168
+ summary: `Restore soft-deleted Splitwise group ${id} — puts it back on balances and notifies its members`,
169
+ method: 'POST',
170
+ path: `/undelete_group/${id}`,
171
+ target: id,
172
+ confirmToken,
173
+ args,
174
+ });
175
+ if (gate)
176
+ return gate;
151
177
  const data = await client.request('POST', `/undelete_group/${id}`);
152
178
  return minifiedResult(data);
153
179
  });
@@ -1,10 +1,27 @@
1
1
  import { z } from 'zod';
2
2
  import { minifiedResult, imageResult, readEnvVar, resolveOutputDir, writeBinaryOutput, sniffMimeBytes, } from '@chrischall/mcp-utils';
3
+ import { UNTRUSTED_DESCRIPTION_SUFFIX, swUntrustedEnvelope } from './_untrusted.js';
3
4
  /** Cap on bytes returned as base64 in the tool result. */
4
5
  const MAX_INLINE_BYTES = 4 * 1024 * 1024;
5
6
  /** Cap on extracted characters, so a long PDF can't flood the result. */
6
7
  const MAX_TEXT_CHARS = 100_000;
7
8
  const PDF_MIME = 'application/pdf';
9
+ /**
10
+ * Names the default output dir, `~/Downloads/splitwise-mcp` (created 0700), used
11
+ * when neither output_dir nor SPLITWISE_OUTPUT_DIR names a place. Not the
12
+ * working directory: under Claude Code that is usually the user's git repo,
13
+ * where a receipt (financial PII) can be committed and pushed by accident
14
+ * (fleet-audit #738).
15
+ */
16
+ const OUTPUT_DIR_NAME = 'splitwise-mcp';
17
+ /**
18
+ * Cap on a receipt download. Bounds the body read (a Content-Length over it is
19
+ * refused before reading; a stream that grows past it is cancelled) so a
20
+ * hostile or broken asset host cannot exhaust memory (fleet-audit #733).
21
+ */
22
+ const MAX_RECEIPT_BYTES = 25 * 1024 * 1024;
23
+ /** Receipts are written owner-only — they carry financial details. */
24
+ const RECEIPT_FILE_MODE = 0o600;
8
25
  /** Content types that carry no information about the actual format. */
9
26
  const GENERIC_TYPES = new Set(['application/octet-stream', 'binary/octet-stream']);
10
27
  /** `%PDF-` magic bytes — `sniffMimeBytes` only knows the image formats. */
@@ -75,12 +92,12 @@ function describeError(err) {
75
92
  }
76
93
  export function registerReceiptTools(server, client) {
77
94
  server.registerTool('sw_get_receipt', {
78
- description: "Download the receipt image or PDF attached to a Splitwise expense. The receipt URLs returned by sw_get_expense need the server's credentials — fetching them directly returns 401 — so use this tool instead. Set inline:true to get the bytes back in the result (images AND PDFs), or extract_text:true to get a PDF's text without the binary at all — both work when the caller can't see this server's filesystem. It also writes the file and returns the path, unless write:false or the filesystem is read-only.",
95
+ description: "Download the receipt image or PDF attached to a Splitwise expense. The receipt URLs returned by sw_get_expense need the server's credentials — fetching them directly returns 401 — so use this tool instead. Set inline:true to get the bytes back in the result (images AND PDFs), or extract_text:true to get a PDF's text without the binary at all — both work when the caller can't see this server's filesystem. Without either, it writes the file (to $SPLITWISE_OUTPUT_DIR, else ~/Downloads/splitwise-mcp) and returns the path; with either, it writes only when output_dir or write:true asks for it. Extracted text: " + UNTRUSTED_DESCRIPTION_SUFFIX,
79
96
  // Not read-only: this writes a file into a caller-supplied directory, and
80
97
  // `readOnlyHint` is what a host reads when deciding to skip its approval
81
98
  // prompt. Not destructive either — `uniquePath` always picks a filename
82
99
  // that doesn't exist, so an existing file is never touched.
83
- annotations: { readOnlyHint: false, destructiveHint: false },
100
+ annotations: { readOnlyHint: false, destructiveHint: false, openWorldHint: true },
84
101
  inputSchema: z.object({
85
102
  id: z.number().describe('Expense ID (the same id sw_get_expense takes)'),
86
103
  size: z
@@ -97,11 +114,11 @@ export function registerReceiptTools(server, client) {
97
114
  .optional(),
98
115
  output_dir: z
99
116
  .string()
100
- .describe('Directory to write the receipt into. Defaults to $SPLITWISE_OUTPUT_DIR, else the current working directory. When SPLITWISE_OUTPUT_DIR is set, this must be inside it.')
117
+ .describe('Directory to write the receipt into. Defaults to $SPLITWISE_OUTPUT_DIR, else ~/Downloads/splitwise-mcp. When SPLITWISE_OUTPUT_DIR is set, this must be inside it.')
101
118
  .optional(),
102
119
  write: z
103
120
  .boolean()
104
- .describe("Write the receipt to disk. Defaults to true. Pass false when the caller cannot reach this server's filesystem, or when the server runs read-only.")
121
+ .describe("Write the receipt to disk (owner-only). Defaults to true, except when inline or extract_text is requested without an output_dir — then the content is returned and nothing is written. Pass false when the caller cannot reach this server's filesystem, or when the server runs read-only.")
105
122
  .optional(),
106
123
  }),
107
124
  }, async ({ id, size, inline, extract_text, output_dir, write }) => {
@@ -114,7 +131,7 @@ export function registerReceiptTools(server, client) {
114
131
  throw new Error(`Splitwise expense ${id} has no receipt attached.`);
115
132
  }
116
133
  const served = receipt?.[wanted] ? wanted : fallback;
117
- const asset = await client.fetchAsset(url);
134
+ const asset = await client.fetchAsset(url, { maxBytes: MAX_RECEIPT_BYTES });
118
135
  if (asset.bytes.length === 0) {
119
136
  throw new Error(`Splitwise returned an empty receipt body for expense ${id}.`);
120
137
  }
@@ -124,9 +141,14 @@ export function registerReceiptTools(server, client) {
124
141
  // filesystem, and failing the whole call there would strand a caller who
125
142
  // only wanted the content. Report it and carry on — unless nothing else
126
143
  // was requested, in which case there is no result to return (below).
144
+ // A caller who asked only for content (inline / extract_text) gets no
145
+ // file unless it named a directory or said write:true — a receipt left
146
+ // on disk that nobody asked for is a PII leak, not a convenience.
147
+ const contentOnly = (inline === true || extract_text === true) && output_dir === undefined;
148
+ const shouldWrite = write ?? !contentOnly;
127
149
  let path;
128
150
  let writeError;
129
- if (write !== false) {
151
+ if (shouldWrite) {
130
152
  try {
131
153
  // output_dir is model-chosen: once the operator configures
132
154
  // SPLITWISE_OUTPUT_DIR, a per-call directory must stay inside it
@@ -135,11 +157,13 @@ export function registerReceiptTools(server, client) {
135
157
  const configuredDir = readEnvVar('SPLITWISE_OUTPUT_DIR');
136
158
  path = writeBinaryOutput({
137
159
  dir: resolveOutputDir(output_dir, 'SPLITWISE_OUTPUT_DIR', {
160
+ name: OUTPUT_DIR_NAME,
138
161
  ...(configuredDir ? { allowedRoots: [configuredDir] } : {}),
139
162
  }),
140
163
  baseName: `splitwise-receipt-${id}`,
141
164
  base64,
142
165
  extension: extensionFor(mimeType, url),
166
+ mode: RECEIPT_FILE_MODE,
143
167
  });
144
168
  }
145
169
  catch (err) {
@@ -192,7 +216,9 @@ export function registerReceiptTools(server, client) {
192
216
  if (path === undefined && !inlined && text === undefined) {
193
217
  const reason = writeError
194
218
  ? `could not be written (${writeError})`
195
- : 'was not written (write:false)';
219
+ : write === false
220
+ ? 'was not written (write:false)'
221
+ : 'was not written (only content was requested; pass write:true or output_dir for a file)';
196
222
  // Name every blocker — more than one can apply at once — then offer only
197
223
  // the routes still open. A flag the caller already passed is not a
198
224
  // suggestion; when `inline` was requested it can only have been rejected
@@ -217,7 +243,7 @@ export function registerReceiptTools(server, client) {
217
243
  advice,
218
244
  ].join(' '));
219
245
  }
220
- const result = minifiedResult({
246
+ const payload = {
221
247
  expense_id: id,
222
248
  size: served,
223
249
  ...(served !== wanted
@@ -245,7 +271,9 @@ export function registerReceiptTools(server, client) {
245
271
  : {}),
246
272
  ...(text !== undefined ? { text } : {}),
247
273
  ...(textNote !== undefined ? { text_note: textNote } : {}),
248
- });
274
+ };
275
+ // The text layer is whatever the uploader put in the PDF — fence it.
276
+ const result = minifiedResult(text !== undefined ? swUntrustedEnvelope(payload) : payload);
249
277
  if (inlined) {
250
278
  const type = mimeType ?? 'application/octet-stream';
251
279
  result.content.push(type.startsWith('image/')
@@ -5,7 +5,7 @@ import { CONFIRM_NOTE, confirmTokenParam, confirmWrite } from './_confirm.js';
5
5
  export function registerUserTools(server, client) {
6
6
  server.registerTool('sw_get_current_user', {
7
7
  description: "Get the authenticated Splitwise user's profile. Use the returned id when building custom expense splits. The compact default returns id, name (first_name + last_name joined), email and registration_status; pass view:'full' for Splitwise's raw record, which keeps first_name and last_name separate — the form sw_update_user takes.",
8
- annotations: { readOnlyHint: true },
8
+ annotations: { readOnlyHint: true, openWorldHint: true },
9
9
  inputSchema: z.object({
10
10
  view: viewParam(SW_VIEWS, { note: PERSON_VIEW_NOTE }),
11
11
  }),
@@ -15,7 +15,7 @@ export function registerUserTools(server, client) {
15
15
  });
16
16
  server.registerTool('sw_get_user', {
17
17
  description: "Get another Splitwise user's profile by id. Same shape as sw_get_current_user: the compact default merges first_name/last_name into name, and view:'full' keeps them separate.",
18
- annotations: { readOnlyHint: true },
18
+ annotations: { readOnlyHint: true, openWorldHint: true },
19
19
  inputSchema: z.object({
20
20
  view: viewParam(SW_VIEWS, { note: PERSON_VIEW_NOTE }),
21
21
  id: z.number().describe('User ID'),
@@ -26,7 +26,7 @@ export function registerUserTools(server, client) {
26
26
  });
27
27
  server.registerTool('sw_update_user', {
28
28
  description: "Update the current user's profile fields: name, locale and default currency. id must be the current user's id. The login email and password are deliberately not settable here — account credentials are changed in the Splitwise app, not by an assistant. " + CONFIRM_NOTE,
29
- annotations: { readOnlyHint: false, destructiveHint: true },
29
+ annotations: { readOnlyHint: false, destructiveHint: true, openWorldHint: true },
30
30
  // No `email` / `password`: changing the login email is an account
31
31
  // takeover primitive (a password reset to the new address follows), and
32
32
  // the only gate would be a model-driven confirmation that injected text from
@@ -41,7 +41,8 @@ export function registerUserTools(server, client) {
41
41
  default_currency: z.string().optional(),
42
42
  confirmToken: confirmTokenParam,
43
43
  }),
44
- }, async ({ id, first_name, last_name, locale, default_currency, confirmToken }, ctx) => {
44
+ }, async (args, ctx) => {
45
+ const { id, first_name, last_name, locale, default_currency, confirmToken } = args;
45
46
  const body = pruneUndefined({
46
47
  first_name,
47
48
  last_name,
@@ -57,6 +58,7 @@ export function registerUserTools(server, client) {
57
58
  body,
58
59
  target: id,
59
60
  confirmToken,
61
+ args,
60
62
  });
61
63
  if (gate)
62
64
  return gate;