Skip to content

armgit / jan

Public Imported from GitHub
0

Jan is an open source alternative to ChatGPT that runs 100% offline on your computer.

fix: mcp tool approval

Louisc856ffe81503
diff --git a/web-app/src/ai-elements/tool.tsx b/web-app/src/ai-elements/tool.tsx
index 2a55526e0..f56cdf1a4 100644
--- a/web-app/src/ai-elements/tool.tsx
+++ b/web-app/src/ai-elements/tool.tsx
@@ -95,7 +95,7 @@ const getStatusText = (status: ToolUIPart['state'], toolName: string) => {
return `Running ${toolName.replaceAll('_', ' ')}...`
}
if (hasError) {
- return `${toolName} failed`
+ return `${toolName.replaceAll('_', ' ')} failed`
}
return `Used ${toolName.replaceAll('_', ' ')}`
}
diff --git a/web-app/src/hooks/useToolApproval.ts b/web-app/src/hooks/useToolApproval.ts
index 13529115d..446cefbc0 100644
--- a/web-app/src/hooks/useToolApproval.ts
+++ b/web-app/src/hooks/useToolApproval.ts
@@ -57,14 +57,15 @@ export const useToolApproval = create<ToolApprovalState>()(
showApprovalModal: (toolName: string, threadId: string, toolParameters?: object) => {
return new Promise<boolean>((resolve) => {
- // Auto-approve MCP tools when feature is enabled
- if (PlatformFeatures[PlatformFeature.MCP_AUTO_APPROVE_TOOLS]) {
+ const state = get()
+
+ // Auto-approve if the user has enabled auto-approval setting
+ if (state.allowAllMCPPermissions) {
resolve(true)
return
}
// Check if tool is already approved for this thread
- const state = get()
if (state.isToolApproved(threadId, toolName)) {
resolve(true)
return
diff --git a/web-app/src/lib/custom-chat-transport.ts b/web-app/src/lib/custom-chat-transport.ts
index b85863e3b..4213935d4 100644
--- a/web-app/src/lib/custom-chat-transport.ts
+++ b/web-app/src/lib/custom-chat-transport.ts
@@ -382,11 +382,11 @@ export class CustomChatTransport implements ChatTransport<UIMessage> {
return messages.map((message) => {
if (message.role === 'user') {
const metadata = message.metadata as
- | { inline_file_contents?: Array<{ name?: string; content?: string }> }
+ | {
+ inline_file_contents?: Array<{ name?: string; content?: string }>
+ }
| undefined
- const inlineFileContents = Array.isArray(
- metadata?.inline_file_contents
- )
+ const inlineFileContents = Array.isArray(metadata?.inline_file_contents)
? metadata.inline_file_contents.filter((f) => f?.content)
: []
// Tool messages have content as array of ToolResultPart
diff --git a/web-app/src/routes/threads/$threadId.tsx b/web-app/src/routes/threads/$threadId.tsx
index b7aaccee7..e00c6336a 100644
--- a/web-app/src/routes/threads/$threadId.tsx
+++ b/web-app/src/routes/threads/$threadId.tsx
@@ -48,6 +48,7 @@ import { useToolAvailable } from '@/hooks/useToolAvailable'
import { OUT_OF_CONTEXT_SIZE } from '@/utils/error'
import { Button } from '@/components/ui/button'
import { IconAlertCircle } from '@tabler/icons-react'
+import { useToolApproval } from '@/hooks/useToolApproval'
const CHAT_STATUS = {
STREAMING: 'streaming',
@@ -216,17 +217,33 @@ function ThreadDetail() {
const ragToolNames = useAppState.getState().ragToolNames
const mcpToolNames = useAppState.getState().mcpToolNames
- // Execute all collected tool calls
- Promise.all(
- // eslint-disable-next-line @typescript-eslint/no-explicit-any
- sessionData.tools.map(async (toolCall: any) => {
+ // Process tool calls sequentially, requesting approval for each if needed
+ ;(async () => {
+ for (const toolCall of sessionData.tools) {
// Check if already aborted before starting
if (signal.aborted) {
- return
+ break
}
try {
const toolName = toolCall.toolName
+
+ // Request approval if needed (unless auto-approve is enabled)
+ const approved = await useToolApproval
+ .getState()
+ .showApprovalModal(toolName, threadId, toolCall.input)
+
+ if (!approved) {
+ // User denied the tool call
+ addToolOutput({
+ state: 'output-error',
+ tool: toolCall.toolName,
+ toolCallId: toolCall.toolCallId,
+ errorText: 'Tool execution denied by user',
+ })
+ continue
+ }
+
let result
// Route to the appropriate service based on tool name
@@ -274,18 +291,19 @@ function ThreadDetail() {
})
}
}
- })
- )
- .catch((error) => {
- // Ignore abort errors
- if (error.name !== 'AbortError') {
- console.error('Tool call error:', error)
- }
- })
- .finally(() => {
- sessionData.tools = []
- toolCallAbortController.current = null
- })
+ }
+
+ // Clear tools after processing all
+ sessionData.tools = []
+ toolCallAbortController.current = null
+ })().catch((error) => {
+ // Ignore abort errors
+ if (error.name !== 'AbortError') {
+ console.error('Tool call error:', error)
+ }
+ sessionData.tools = []
+ toolCallAbortController.current = null
+ })
},
onToolCall: ({ toolCall }) => {
sessionData.tools.push(toolCall)
@@ -683,11 +701,6 @@ function ThreadDetail() {
{chatMessages.map((message, index) => {
const isLastMessage = index === chatMessages.length - 1
const isFirstMessage = index === 0
- // const showAssistant =
- // message.role === 'assistant' &&
- // (isFirstMessage ||
- // chatMessages[index - 1]?.role !== 'assistant')
-
return (
<MessageItem
key={message.id}
@@ -697,15 +710,6 @@ function ThreadDetail() {
status={status}
reasoningContainerRef={reasoningContainerRef}
onRegenerate={handleRegenerate}
- // showAssistant={showAssistant}
- // assistant={
- // currentAssistant
- // ? {
- // avatar: currentAssistant.avatar,
- // name: currentAssistant.name,
- // }
- // : undefined
- // }
/>
)
})}