Skip to content

Commit 969da37

Browse files
tleydenclaude
andauthored
Refresh oauth tokens (#141)
* Refresh oauth token * update version number to something sensible * add name to license * mcp build server * logfire settings * bump version * dropi access token for easy testing * refresh token button * better logging when refresh * Add resource param to OAuth token refresh and exchange; bump version to 0.3.4 Per MCP spec, the resource URI must be included in all token requests. Also adds client_id/secret hashes to refresh logs for credential debugging. Co-Authored-By: Claude Sonnet 4.6 <noreply@anthropic.com> Claude-Session: https://claude.ai/code/session_01J7NXmkDMaSkftFFEdcxVjs * Fix OAuth refresh to use client_secret_post instead of client_secret_basic expo-auth-session sends credentials via Authorization Basic header when clientSecret is set directly. brain3-dev only accepts client_secret_post (credentials in POST body). Move client_secret into extraParams to force body inclusion. Co-Authored-By: Claude Sonnet 4.6 <noreply@anthropic.com> Claude-Session: https://claude.ai/code/session_01J7NXmkDMaSkftFFEdcxVjs --------- Co-authored-by: Claude Sonnet 4.6 <noreply@anthropic.com>
1 parent c8ffd26 commit 969da37

17 files changed

Lines changed: 1050 additions & 31 deletions

‎.codex/config.toml‎

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
[mcp_servers.arty-build-eas]
2+
command = "bun"
3+
args = [
4+
"run",
5+
"mcp-server/index.ts",
6+
]

‎AGENTS.md‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
- When logging, default to logging full values and not substrings. Later we can prune as needed, but right now there is an observability crisis.
66
- Certain swift objects like AVAudioPlayerDelegate should have serialized access when called from multiple threads. Consider protecting shared state with a serial DispatchQueue or ensuring all method calls dispatch to the main queue.
77
- Prefer convention over configuration, this keeps things light, de-coupled, and avoids needless boilerplate.
8+
- In logfire, the project is using the old name: vibemachine
89

910
Always use expo libraries, for example:
1011

‎LICENSE‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -186,7 +186,7 @@
186186
same "printed page" as the copyright notice for easier
187187
identification within third-party archives.
188188

189-
Copyright 2025 Vibemachine LLC
189+
Copyright 2025 Traun Leyden
190190

191191
Licensed under the Apache License, Version 2.0 (the "License");
192192
you may not use this file except in compliance with the License.

‎app.json‎

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33
"owner": "tleyden",
44
"name": "vibemachine",
55
"slug": "vibemachine",
6-
"version": "1.0.0",
6+
"version": "0.3.4",
77
"orientation": "portrait",
88
"icon": "./assets/images/icon.png",
99
"scheme": "vibemachine",
@@ -78,7 +78,7 @@
7878
"projectId": "f3a6322c-ba58-4e74-902e-89a4d9fccb52"
7979
}
8080
},
81-
"runtimeVersion": "1.0.0",
81+
"runtimeVersion": "0.3.4",
8282
"updates": {
8383
"url": "https://u.expo.dev/f3a6322c-ba58-4e74-902e-89a4d9fccb52"
8484
}

‎app/index.tsx‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -765,6 +765,7 @@ export default function Index() {
765765
visible={mcpExtensionsVisible}
766766
onClose={() => setMcpExtensionsVisible(false)}
767767
onBeforeBrowserOpen={() => {
768+
log.info("[index] onBeforeBrowserOpen: hiding McpExtensionsScreen before OAuth — will NOT auto-reopen on success");
768769
setMcpExtensionsVisible(false);
769770
setMenuVisible(false);
770771
}}

‎components/settings/McpConnectorConfig.tsx‎

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -301,6 +301,7 @@ export const McpConnectorConfig: React.FC<McpConnectorConfigProps> = ({
301301
});
302302

303303
try {
304+
log.info("[mcp_connector] Calling onBeforeBrowserOpen (static) — parent screen will be hidden", {}, { id });
304305
onBeforeBrowserOpen?.();
305306
const oauthResult = await performMcpOAuthFlow(
306307
id,
@@ -311,6 +312,7 @@ export const McpConnectorConfig: React.FC<McpConnectorConfigProps> = ({
311312
);
312313
log.info("[mcp_connector] Static OAuth flow returned", {}, { oauthResult_type: oauthResult.type });
313314
if (oauthResult.type === "success") {
315+
log.info("[mcp_connector] Static OAuth success — no re-open of parent screen on success path", {}, { id });
314316
await persistExtension(id, sanitizedForm, undefined, {
315317
preserveExistingToken: true,
316318
authMode: "static",
@@ -358,6 +360,7 @@ export const McpConnectorConfig: React.FC<McpConnectorConfigProps> = ({
358360
},
359361
);
360362
try {
363+
log.info("[mcp_connector] Calling onBeforeBrowserOpen (dcr) — parent screen will be hidden", {}, { id });
361364
onBeforeBrowserOpen?.();
362365
const oauthResult = await performMcpOAuthFlow(
363366
id,
@@ -370,6 +373,7 @@ export const McpConnectorConfig: React.FC<McpConnectorConfigProps> = ({
370373
);
371374
log.info("[mcp_connector] OAuth flow returned", {}, { oauthResult_type: oauthResult.type });
372375
if (oauthResult.type === "success") {
376+
log.info("[mcp_connector] DCR OAuth success — no re-open of parent screen on success path", {}, { id });
373377
await persistExtension(id, sanitizedForm, undefined, {
374378
preserveExistingToken: true,
375379
authMode: "dcr",

‎components/settings/McpExtensionDetailScreen.tsx‎

Lines changed: 179 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,8 @@ import {
2727
getMcpBearerToken,
2828
type McpExtensionRecord,
2929
} from "../../lib/secure-storage";
30+
import { refreshMcpAccessTokenWithDetails } from "../../lib/mcp-oauth";
31+
import { log } from "../../lib/logger";
3032
import { CONNECTOR_SETTINGS_CHANGED_EVENT } from "../../modules/vm-webrtc/src/ToolkitManager";
3133
import { McpConnectorConfig } from "./McpConnectorConfig";
3234

@@ -52,6 +54,7 @@ export const McpExtensionDetailScreen: React.FC<McpExtensionDetailScreenProps> =
5254
const [toolsError, setToolsError] = useState<string | null>(null);
5355
const [configureVisible, setConfigureVisible] = useState(false);
5456
const [urlCopied, setUrlCopied] = useState(false);
57+
const [refreshingAccessToken, setRefreshingAccessToken] = useState(false);
5558

5659
useEffect(() => {
5760
setCurrentExtension(extension);
@@ -162,6 +165,98 @@ export const McpExtensionDetailScreen: React.FC<McpExtensionDetailScreenProps> =
162165
}
163166
};
164167

168+
const handleRefreshAccessToken = async () => {
169+
if (refreshingAccessToken) return;
170+
171+
setRefreshingAccessToken(true);
172+
try {
173+
log.info(
174+
"[mcp_detail] Manual access token refresh requested",
175+
{},
176+
{
177+
extension_id: currentExtension.id,
178+
extension_name: currentExtension.name,
179+
server_url: currentExtension.serverUrl,
180+
}
181+
);
182+
183+
const result = await refreshMcpAccessTokenWithDetails(
184+
currentExtension.id,
185+
currentExtension.name
186+
);
187+
if (result.type === "failure") {
188+
log.warn(
189+
"[mcp_detail] Manual access token refresh failed",
190+
{},
191+
{
192+
extension_id: currentExtension.id,
193+
extension_name: currentExtension.name,
194+
user_message: result.userMessage,
195+
oauth_error_code: result.oauthErrorCode,
196+
}
197+
);
198+
Alert.alert("Refresh Failed", result.userMessage, [{ text: "OK" }]);
199+
return;
200+
}
201+
202+
DeviceEventEmitter.emit(CONNECTOR_SETTINGS_CHANGED_EVENT);
203+
fetchTools();
204+
Alert.alert("Access Token Refreshed", "A new access token was saved.", [{ text: "OK" }]);
205+
} catch (err) {
206+
const message = err instanceof Error ? err.message : String(err);
207+
log.error(
208+
"[mcp_detail] Manual access token refresh threw unexpected error",
209+
{},
210+
{
211+
extension_id: currentExtension.id,
212+
extension_name: currentExtension.name,
213+
error_name: err instanceof Error ? err.name : undefined,
214+
error_message: message,
215+
error_stack: err instanceof Error ? err.stack : undefined,
216+
}
217+
);
218+
Alert.alert(
219+
"Refresh Failed",
220+
`Could not refresh the access token: ${message}`,
221+
[{ text: "OK" }]
222+
);
223+
} finally {
224+
setRefreshingAccessToken(false);
225+
}
226+
};
227+
228+
const handleResetAccessToken = () => {
229+
const doReset = async () => {
230+
await deleteMcpBearerToken(currentExtension.id);
231+
DeviceEventEmitter.emit(CONNECTOR_SETTINGS_CHANGED_EVENT);
232+
};
233+
234+
if (Platform.OS === "ios") {
235+
ActionSheetIOS.showActionSheetWithOptions(
236+
{
237+
title: `Reset access token for "${currentExtension.name}"?`,
238+
message:
239+
"The access token will be dropped. The app will use the refresh token on the next MCP call.",
240+
options: ["Cancel", "Reset Access Token"],
241+
destructiveButtonIndex: 1,
242+
cancelButtonIndex: 0,
243+
},
244+
(buttonIndex) => {
245+
if (buttonIndex === 1) doReset();
246+
}
247+
);
248+
} else {
249+
Alert.alert(
250+
"Reset Access Token",
251+
`Drop the access token for "${currentExtension.name}"? The app will use the refresh token on the next MCP call.`,
252+
[
253+
{ text: "Cancel", style: "cancel" },
254+
{ text: "Reset Access Token", style: "destructive", onPress: doReset },
255+
]
256+
);
257+
}
258+
};
259+
165260
return (
166261
<Modal
167262
animationType="slide"
@@ -270,23 +365,58 @@ export const McpExtensionDetailScreen: React.FC<McpExtensionDetailScreenProps> =
270365
</ScrollView>
271366

272367
<View style={[styles.footer, { paddingBottom: insets.bottom + 16 }]}>
368+
<View style={styles.footerPrimaryActions}>
369+
<Pressable
370+
style={({ pressed }) => [
371+
styles.configureButton,
372+
pressed && styles.configureButtonPressed,
373+
]}
374+
onPress={() => setConfigureVisible(true)}
375+
>
376+
<Text style={styles.configureButtonText}>Configure</Text>
377+
</Pressable>
378+
<Pressable
379+
style={({ pressed }) => [
380+
styles.resetAuthButton,
381+
pressed && styles.resetAuthButtonPressed,
382+
]}
383+
onPress={handleResetAuth}
384+
>
385+
<Text style={styles.resetAuthButtonText}>Reset Auth</Text>
386+
</Pressable>
387+
<Pressable
388+
style={({ pressed }) => [
389+
styles.removeButton,
390+
pressed && styles.removeButtonPressed,
391+
]}
392+
onPress={handleRemove}
393+
>
394+
<Text style={styles.removeButtonText}>Remove</Text>
395+
</Pressable>
396+
</View>
273397
<Pressable
274-
style={({ pressed }) => [styles.configureButton, pressed && styles.configureButtonPressed]}
275-
onPress={() => setConfigureVisible(true)}
276-
>
277-
<Text style={styles.configureButtonText}>Configure</Text>
278-
</Pressable>
279-
<Pressable
280-
style={({ pressed }) => [styles.resetAuthButton, pressed && styles.resetAuthButtonPressed]}
281-
onPress={handleResetAuth}
398+
style={({ pressed }) => [
399+
styles.resetAccessTokenButton,
400+
pressed && styles.resetAccessTokenButtonPressed,
401+
]}
402+
onPress={handleResetAccessToken}
282403
>
283-
<Text style={styles.resetAuthButtonText}>Reset Auth</Text>
404+
<Text style={styles.resetAccessTokenButtonText}>Reset Access Token</Text>
284405
</Pressable>
285406
<Pressable
286-
style={({ pressed }) => [styles.removeButton, pressed && styles.removeButtonPressed]}
287-
onPress={handleRemove}
407+
disabled={refreshingAccessToken}
408+
style={({ pressed }) => [
409+
styles.refreshAccessTokenButton,
410+
pressed && styles.refreshAccessTokenButtonPressed,
411+
refreshingAccessToken && styles.refreshAccessTokenButtonDisabled,
412+
]}
413+
onPress={handleRefreshAccessToken}
288414
>
289-
<Text style={styles.removeButtonText}>Remove</Text>
415+
{refreshingAccessToken ? (
416+
<ActivityIndicator size="small" color="#FFFFFF" />
417+
) : (
418+
<Text style={styles.refreshAccessTokenButtonText}>Refresh Access Token</Text>
419+
)}
290420
</Pressable>
291421
</View>
292422
</SafeAreaView>
@@ -497,14 +627,17 @@ const styles = StyleSheet.create({
497627
lineHeight: 17,
498628
},
499629
footer: {
500-
flexDirection: "row",
501630
gap: 12,
502631
paddingHorizontal: 20,
503632
paddingTop: 16,
504633
borderTopWidth: 1,
505634
borderTopColor: "#E5E5EA",
506635
backgroundColor: "#F5F5F7",
507636
},
637+
footerPrimaryActions: {
638+
flexDirection: "row",
639+
gap: 12,
640+
},
508641
configureButton: {
509642
flex: 1,
510643
paddingVertical: 14,
@@ -554,4 +687,37 @@ const styles = StyleSheet.create({
554687
fontWeight: "600",
555688
color: "#FF3B30",
556689
},
690+
resetAccessTokenButton: {
691+
paddingVertical: 14,
692+
borderRadius: 12,
693+
alignItems: "center",
694+
backgroundColor: "#FFFFFF",
695+
borderWidth: 1,
696+
borderColor: "#0A84FF",
697+
},
698+
resetAccessTokenButtonPressed: {
699+
backgroundColor: "#F0F6FF",
700+
},
701+
resetAccessTokenButtonText: {
702+
fontSize: 16,
703+
fontWeight: "600",
704+
color: "#0A84FF",
705+
},
706+
refreshAccessTokenButton: {
707+
paddingVertical: 14,
708+
borderRadius: 12,
709+
alignItems: "center",
710+
backgroundColor: "#0A84FF",
711+
},
712+
refreshAccessTokenButtonPressed: {
713+
backgroundColor: "#006EDB",
714+
},
715+
refreshAccessTokenButtonDisabled: {
716+
opacity: 0.65,
717+
},
718+
refreshAccessTokenButtonText: {
719+
fontSize: 16,
720+
fontWeight: "600",
721+
color: "#FFFFFF",
722+
},
557723
});

‎components/settings/McpExtensionsScreen.tsx‎

Lines changed: 16 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@ import {
1616
type McpExtensionRecord,
1717
} from "../../lib/secure-storage";
1818
import { MCP_REAUTH_REQUIRED_EVENT } from "../../modules/vm-webrtc/src/ToolkitManager";
19+
import { log } from "../../lib/logger";
1920
import { McpConnectorConfig } from "./McpConnectorConfig";
2021
import { McpExtensionDetailScreen } from "./McpExtensionDetailScreen";
2122

@@ -53,15 +54,28 @@ export const McpExtensionsScreen: React.FC<McpExtensionsScreenProps> = ({
5354
const sub = DeviceEventEmitter.addListener(
5455
MCP_REAUTH_REQUIRED_EVENT,
5556
({ id, name }: { id: string; name: string }) => {
57+
log.info("[McpExtensionsScreen] MCP_REAUTH_REQUIRED_EVENT received", {}, { id, name });
5658
const ext = extensionsRef.current.find((e) => e.id === id);
59+
log.info(
60+
"[McpExtensionsScreen] Showing reauth alert",
61+
{},
62+
{ id, name, ext_found_in_ref: !!ext, ref_length: extensionsRef.current.length },
63+
);
5764
Alert.alert(
5865
`"${name}" needs to sign in again`,
5966
"Your session has expired.",
6067
[
61-
{ text: "Later", style: "cancel" },
68+
{
69+
text: "Later",
70+
style: "cancel",
71+
onPress: () => log.info("[McpExtensionsScreen] Reauth alert dismissed (Later)", {}, { id, name }),
72+
},
6273
{
6374
text: "Re-authenticate",
64-
onPress: () => setReauthExtension(ext ?? { id, name, normalizedName: "", serverUrl: "" }),
75+
onPress: () => {
76+
log.info("[McpExtensionsScreen] Reauth alert accepted, opening re-auth flow", {}, { id, name });
77+
setReauthExtension(ext ?? { id, name, normalizedName: "", serverUrl: "" });
78+
},
6579
},
6680
],
6781
);

0 commit comments

Comments
 (0)