Skip to content

Commit 7ef0971

Browse files
authored
Add Tasks support (modelcontextprotocol#1013)
* ### client/src/App.tsx - Imports: - Added: `Task` and `GetTaskResultSchema` to the same import block. - UI Icons: Added `ListTodo` from `lucide-react` to the icon imports. - Components: Added `TasksTab` import beside `ToolsTab`. - Config utils: Added `getMCPTaskTtl` to the config utils import block. - State additions: - `const [tasks, setTasks] = useState<Task[]>([]);` - Extended `errors` state to include a `tasks` key: `tasks: null,` - `const [selectedTask, setSelectedTask] = useState<Task | null>(null);` - `const [isPollingTask, setIsPollingTask] = useState(false);` - `const [nextTaskCursor, setNextTaskCursor] = useState<string | undefined>();` - Hook: `useConnection({...})` return value usage extended - Added destructured functions: `cancelTask: cancelMcpTask` and `listTasks: listMcpTasks` from the custom hook. - Notification handling: - In `onNotification`, added: - If `method === "notifications/tasks/list_changed"`, call `listTasks()` (voided). - If `method === "notifications/tasks/status"`, treat `notification.params` as a `Task` and update `tasks` state: replace if exists by `taskId`, otherwise prepend. Also update `selectedTask` if it’s the same `taskId`. - Tab routing: - When computing valid tabs, added `tasks` when server declares capability: `...(serverCapabilities?.tasks ? ["tasks"] : []),` - When choosing a default tab, added a branch to fall back to `"tasks"` if neither resources/prompts/tools are present but tasks are. - Effect for Tasks tab: - When `mcpClient` is connected and `activeTab === "tasks"`, invoke `listTasks()`. - Tools → task-augmented calls integration in `callTool`: - Parameter signature supports `runAsTask?: boolean` (already present in this file), but now: - If `runAsTask` is true, augment the `tools/call` request’s `params` with a `task` object: `{ task: { ttl: getMCPTaskTtl(config) } }`. - Use a permissive result schema for tool call: `sendMCPRequest(request, z.any(), "tools")` to avoid version-mismatch schema issues. - Task reference detection introduced: - `isTaskResult` helper checks for a nested `task` object with `taskId` (i.e., `response.task.taskId`). - When task is detected: - Set `isPollingTask(true)`. - Immediately set a temporary `toolResult` that includes text content “Task created: … Polling for status…” and `_meta` with `"io.modelcontextprotocol/related-task": { taskId }`. - Start a polling loop: - Delay 1s between polls. - Call `tasks/get` with `GetTaskResultSchema` for status. - If status is `completed`: call `tasks/result` with `z.any()` to retrieve the final result and set it as `toolResult`; call `listTasks()`. - If status is `failed` or `cancelled`: set an error `toolResult` content that includes the status + `statusMessage`; call `listTasks()`. - Else (still running): update `toolResult` content with current `status`/`statusMessage` and preserve `_meta` related-task. - After loop, set `isPollingTask(false)`. - When not a task response, set `toolResult` directly from response (cast to `CompatibilityCallToolResult`). - Tasks list + cancel helpers in App: - `listTasks`: uses `listMcpTasks(nextTaskCursor)` from the hook, updates `tasks`, `nextTaskCursor`, and clears `errors.tasks`. - `cancelTask`: calls `cancelMcpTask(taskId)`, updates `tasks` array by `taskId`, updates `selectedTask` if it matches, and clears `errors.tasks`. - UI integration: - Added a `TabsTrigger` for “Tasks” with `<ListTodo />` icon, disabled unless server supports tasks. - Added `<TasksTab />` to the main `TabsContent` block, passing: `tasks`, `listTasks`, `clearTasks`, `cancelTask`, `selectedTask`, `setSelectedTask`, `error={errors.tasks}`, `nextCursor={nextTaskCursor}`. - Passed `isPollingTask={isPollingTask}` and `toolResult` into `ToolsTab` so the Tools tab can show the live “Polling Task…” state and block reruns while polling. Note: The raw diff is long; the key hunks align with the above bullet points (imports, state, notifications, tab wiring, request augmentation, polling loop, UI additions). --- ### client/src/components/ToolsTab.tsx - Props shape changed: - Added `isPollingTask?: boolean` prop in the destructured props and in the prop types. - The `callTool` callback signature is now `(name, params, metadata?, runAsTask?) => Promise<void>` (runAsTask added earlier; test updates elsewhere reflect this). - Local state additions: - `const [runAsTask, setRunAsTask] = useState(false);` - Reset behavior: - When switching tools (`useEffect` on `selectedTool`), reset `runAsTask(false)`. - When clearing the list in `ListPane.clearItems`, also call `setRunAsTask(false)`. - UI additions: - New checkbox control block to toggle “Run as task”: - Checkbox `id="run-as-task"`, bound to `runAsTask`, with `onCheckedChange` → `setRunAsTask(checked)`. - Label “Run as task”. - Run button disabling conditions expanded to include `isPollingTask`. - Run button text shows spinner with conditional label: - If `isToolRunning || isPollingTask` → show spinner and text `isPollingTask ? "Polling Task..." : "Running..."`. - Call invocation change: - When clicking “Run Tool”, the `callTool` is invoked with `(selectedTool.name, params, metadata?, runAsTask)`. - ToolResults relay: - Passes `isPollingTask` to `<ToolResults />`. --- ### client/src/components/ToolResults.tsx - Props shape changed: - Added optional prop: `isPollingTask?: boolean`. - Task-running banner logic: - Extracts related task from the tool result’s `_meta["io.modelcontextprotocol/related-task"]` if present. - Computes `isTaskRunning` as `isPollingTask ||` a text-heuristic against `structuredResult.content` entries that contain text like “Polling” or “Task status”. - Header “Tool Result:” now conditionally shows: - `Error` (red) if `isError` is true, else - `Task Running` (yellow) if `isTaskRunning`, else - `Success` (green). No other changes to validation or rendering of content blocks. --- ### client/src/components/TasksTab.tsx (new file) - A brand new tab to list and inspect tasks. - Key elements: - Imports `Task` type and multiple status icons. - `TaskStatusIcon` component maps task `status` to an icon and color. - Main `TasksTab` props: `tasks`, `listTasks`, `clearTasks`, `cancelTask`, `selectedTask`, `setSelectedTask`, `error`, `nextCursor`. - Left column (`ListPane`): lists tasks, shows status icon, `taskId`, `status`, and last update time; button text changes to “List More Tasks” if `nextCursor` present; disables button if no cursor and list non-empty. - Right column: - Shows error `Alert` if `error` prop provided. - If a task is selected: header with `Task Details`, a Cancel button when `status === "working"` (shows a spinner while cancelling), and a grid of task fields: Status (with colored label and icon), Last Updated, Created At, TTL (shows “Infinite” if `ttl === null`, otherwise shows numeric with `s` suffix), optional Status Message, and full task JSON via `JsonView`. - If no task is selected: centered empty state with a “Refresh Tasks” button. --- ### client/src/lib/hooks/useConnection.ts - Imports added from `@modelcontextprotocol/sdk/types.js`: - `ListTasksResultSchema`, `CancelTaskResultSchema`, `TaskStatusNotificationSchema`. - Client capabilities on `connect`: - Added `tasks: { list: {}, cancel: {} }` into the `clientCapabilities` passed to `new Client(...)`. - Notification handling setup: - The hook’s notification schema registration now includes the `TaskStatusNotificationSchema` in the `setNotificationHandler` list so the app receives `notifications/tasks/status`. - New hook functions: - `cancelTask(taskId: string)` sends `tasks/cancel` with `CancelTaskResultSchema`. - `listTasks(cursor?: string)` sends `tasks/list` with `ListTasksResultSchema`. - Exports: - Returned object now includes `cancelTask` and `listTasks`. --- ### client/src/utils/configUtils.ts - Added a new getter: - `export const getMCPTaskTtl = (config: InspectorConfig): number => { return config.MCP_TASK_TTL.value as number; };` --- ### client/src/lib/configurationTypes.ts - `InspectorConfig` type extended with a new item: - `MCP_TASK_TTL: ConfigItem;` - Includes descriptive JSDoc about default TTL in milliseconds for newly created tasks. --- ### client/src/lib/constants.ts - `DEFAULT_INSPECTOR_CONFIG` extended with a default for task TTL: - Key: `MCP_TASK_TTL` - Label: `"Task TTL"` - Description: `"Default Time-to-Live (TTL) in milliseconds for newly created tasks"` - Default `value: 60000` - `is_session_item: false` --- ### client/src/components/__tests__/ToolsTab.test.tsx - Expectations updated due to new `callTool` signature (4th arg `runAsTask`). Everywhere the test asserts a `callTool` invocation, an additional trailing `false` argument was added to reflect the default state when the box isn’t checked. - Examples of added trailing `false` at various assertion points (line offsets from diff): after calls around prior lines 132, 157, 193, 236, 257, 279, 297, 818, 1082 (now passing 4 arguments: name, params, metadata-or-undefined, false). --- ### Additional notes - The Tasks feature is wired end-to-end: - Client capability declaration (list/cancel) - Notification handler for `notifications/tasks/status` - Tools call augmentation with `task` `{ ttl }` - Polling loop using `tasks/get` and `tasks/result` - UI feedback in Tools and dedicated Tasks tab - Configurable TTL via new config item and getter * Fix issue where displayed task on task tab was not updated when task completes, leaving the cancel task button available. * Handle button the same when input_requred or working state * ### What changed - Added **receiver-side (client-side) Tasks support** to the Inspector client for task-augmented **server → client** requests. - When the server sends `sampling/createMessage` or `elicitation/create` with `params.task`, the client now: - Creates a local task and immediately returns a `CreateTaskResult` (`{ task: ... }`). - Runs the underlying user-driven flow asynchronously. - Updates task state to `completed` / `failed` and makes the final payload available via `tasks/result`. - Implemented server polling handlers backed by the local task store: - `tasks/get` - `tasks/result` (blocks until payload is ready) - `tasks/list` - `tasks/cancel` - Advertises task-augmented request capabilities via `capabilities.tasks.requests.sampling.createMessage` and `capabilities.tasks.requests.elicitation.create` when the corresponding callbacks are provided. - Emits best-effort `notifications/tasks/status` updates as local task status changes. * Fix review requests. * In the ToolsTab, when a tool call results in an elicitation request, the app automatically switches to the elicitation tab, and once submitted, switches back to the ToolsTab to view the result of the tool call. However, when a tool call results in a sampling request, it does not switch automatically to the SamplingTab and when the sampling request form is submitted it does not automatically switch back to the ToolsTab to view the result of the tool call. These changes make the sampling use case work like the elicitation one. Tests were added.
1 parent 6dc5d1a commit 7ef0971

File tree

13 files changed

+1479
-108
lines changed

13 files changed

+1479
-108
lines changed

client/src/App.tsx

Lines changed: 308 additions & 13 deletions
Large diffs are not rendered by default.
Lines changed: 239 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,239 @@
1+
import {
2+
act,
3+
fireEvent,
4+
render,
5+
screen,
6+
waitFor,
7+
} from "@testing-library/react";
8+
import App from "../App";
9+
import { useConnection } from "../lib/hooks/useConnection";
10+
import type { Client } from "@modelcontextprotocol/sdk/client/index.js";
11+
import type {
12+
CreateMessageRequest,
13+
CreateMessageResult,
14+
} from "@modelcontextprotocol/sdk/types.js";
15+
16+
type OnPendingRequestHandler = (
17+
request: CreateMessageRequest,
18+
resolve: (result: CreateMessageResult) => void,
19+
reject: (error: Error) => void,
20+
) => void;
21+
22+
type SamplingRequestMockProps = {
23+
request: { id: number };
24+
onApprove: (id: number, result: CreateMessageResult) => void;
25+
onReject: (id: number) => void;
26+
};
27+
28+
type UseConnectionReturn = ReturnType<typeof useConnection>;
29+
30+
// Mock auth dependencies first
31+
jest.mock("@modelcontextprotocol/sdk/client/auth.js", () => ({
32+
auth: jest.fn(),
33+
}));
34+
35+
jest.mock("../lib/oauth-state-machine", () => ({
36+
OAuthStateMachine: jest.fn(),
37+
}));
38+
39+
jest.mock("../lib/auth", () => ({
40+
InspectorOAuthClientProvider: jest.fn().mockImplementation(() => ({
41+
tokens: jest.fn().mockResolvedValue(null),
42+
clear: jest.fn(),
43+
})),
44+
DebugInspectorOAuthClientProvider: jest.fn(),
45+
}));
46+
47+
jest.mock("../utils/configUtils", () => ({
48+
...jest.requireActual("../utils/configUtils"),
49+
getMCPProxyAddress: jest.fn(() => "http://localhost:6277"),
50+
getMCPProxyAuthToken: jest.fn(() => ({
51+
token: "",
52+
header: "X-MCP-Proxy-Auth",
53+
})),
54+
getInitialTransportType: jest.fn(() => "stdio"),
55+
getInitialSseUrl: jest.fn(() => "http://localhost:3001/sse"),
56+
getInitialCommand: jest.fn(() => "mcp-server-everything"),
57+
getInitialArgs: jest.fn(() => ""),
58+
initializeInspectorConfig: jest.fn(() => ({})),
59+
saveInspectorConfig: jest.fn(),
60+
}));
61+
62+
jest.mock("../lib/hooks/useDraggablePane", () => ({
63+
useDraggablePane: () => ({
64+
height: 300,
65+
handleDragStart: jest.fn(),
66+
}),
67+
useDraggableSidebar: () => ({
68+
width: 320,
69+
isDragging: false,
70+
handleDragStart: jest.fn(),
71+
}),
72+
}));
73+
74+
jest.mock("../components/Sidebar", () => ({
75+
__esModule: true,
76+
default: () => <div>Sidebar</div>,
77+
}));
78+
79+
jest.mock("../lib/hooks/useToast", () => ({
80+
useToast: () => ({ toast: jest.fn() }),
81+
}));
82+
83+
// Keep the test focused on navigation; avoid DynamicJsonForm/schema complexity.
84+
jest.mock("../components/SamplingRequest", () => ({
85+
__esModule: true,
86+
default: ({ request, onApprove, onReject }: SamplingRequestMockProps) => (
87+
<div data-testid="sampling-request">
88+
<div>sampling-request-{request.id}</div>
89+
<button
90+
type="button"
91+
onClick={() =>
92+
onApprove(request.id, {
93+
model: "stub-model",
94+
stopReason: "endTurn",
95+
role: "assistant",
96+
content: { type: "text", text: "" },
97+
})
98+
}
99+
>
100+
Approve
101+
</button>
102+
<button type="button" onClick={() => onReject(request.id)}>
103+
Reject
104+
</button>
105+
</div>
106+
),
107+
}));
108+
109+
// Mock fetch
110+
global.fetch = jest.fn().mockResolvedValue({ json: () => Promise.resolve({}) });
111+
112+
jest.mock("../lib/hooks/useConnection", () => ({
113+
useConnection: jest.fn(),
114+
}));
115+
116+
describe("App - Sampling auto-navigation", () => {
117+
const mockUseConnection = jest.mocked(useConnection);
118+
119+
const baseConnectionState = {
120+
connectionStatus: "connected" as const,
121+
serverCapabilities: { tools: { listChanged: true, subscribe: true } },
122+
mcpClient: {
123+
request: jest.fn(),
124+
notification: jest.fn(),
125+
close: jest.fn(),
126+
} as unknown as Client,
127+
requestHistory: [],
128+
clearRequestHistory: jest.fn(),
129+
makeRequest: jest.fn(),
130+
sendNotification: jest.fn(),
131+
handleCompletion: jest.fn(),
132+
completionsSupported: false,
133+
connect: jest.fn(),
134+
disconnect: jest.fn(),
135+
serverImplementation: null,
136+
cancelTask: jest.fn(),
137+
listTasks: jest.fn(),
138+
};
139+
140+
beforeEach(() => {
141+
jest.restoreAllMocks();
142+
window.location.hash = "#tools";
143+
});
144+
145+
test("switches to #sampling when a sampling request arrives and switches back to #tools after approve", async () => {
146+
let capturedOnPendingRequest: OnPendingRequestHandler | undefined;
147+
148+
mockUseConnection.mockImplementation((options) => {
149+
capturedOnPendingRequest = (
150+
options as { onPendingRequest?: OnPendingRequestHandler }
151+
).onPendingRequest;
152+
return baseConnectionState as unknown as UseConnectionReturn;
153+
});
154+
155+
render(<App />);
156+
157+
// Ensure we start on tools.
158+
await waitFor(() => {
159+
expect(window.location.hash).toBe("#tools");
160+
});
161+
162+
const resolve = jest.fn();
163+
const reject = jest.fn();
164+
165+
act(() => {
166+
if (!capturedOnPendingRequest) {
167+
throw new Error("Expected onPendingRequest to be provided");
168+
}
169+
170+
capturedOnPendingRequest(
171+
{
172+
method: "sampling/createMessage",
173+
params: { messages: [], maxTokens: 1 },
174+
},
175+
resolve,
176+
reject,
177+
);
178+
});
179+
180+
await waitFor(() => {
181+
expect(window.location.hash).toBe("#sampling");
182+
expect(screen.getByTestId("sampling-request")).toBeTruthy();
183+
});
184+
185+
fireEvent.click(screen.getByText("Approve"));
186+
187+
await waitFor(() => {
188+
expect(resolve).toHaveBeenCalled();
189+
expect(window.location.hash).toBe("#tools");
190+
});
191+
});
192+
193+
test("switches back to #tools after reject", async () => {
194+
let capturedOnPendingRequest: OnPendingRequestHandler | undefined;
195+
196+
mockUseConnection.mockImplementation((options) => {
197+
capturedOnPendingRequest = (
198+
options as { onPendingRequest?: OnPendingRequestHandler }
199+
).onPendingRequest;
200+
return baseConnectionState as unknown as UseConnectionReturn;
201+
});
202+
203+
render(<App />);
204+
205+
await waitFor(() => {
206+
expect(window.location.hash).toBe("#tools");
207+
});
208+
209+
const resolve = jest.fn();
210+
const reject = jest.fn();
211+
212+
act(() => {
213+
if (!capturedOnPendingRequest) {
214+
throw new Error("Expected onPendingRequest to be provided");
215+
}
216+
217+
capturedOnPendingRequest(
218+
{
219+
method: "sampling/createMessage",
220+
params: { messages: [], maxTokens: 1 },
221+
},
222+
resolve,
223+
reject,
224+
);
225+
});
226+
227+
await waitFor(() => {
228+
expect(window.location.hash).toBe("#sampling");
229+
expect(screen.getByTestId("sampling-request")).toBeTruthy();
230+
});
231+
232+
fireEvent.click(screen.getByText("Reject"));
233+
234+
await waitFor(() => {
235+
expect(reject).toHaveBeenCalled();
236+
expect(window.location.hash).toBe("#tools");
237+
});
238+
});
239+
});

client/src/components/SamplingTab.tsx

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@ import SamplingRequest from "./SamplingRequest";
99
export type PendingRequest = {
1010
id: number;
1111
request: CreateMessageRequest;
12+
originatingTab?: string;
1213
};
1314

1415
export type Props = {

0 commit comments

Comments
 (0)