Skip to content

Commit 3b07278

Browse files
committed
feat(server): add element shaping utility for dynamic detail control
1 parent 1083a56 commit 3b07278

File tree

2 files changed

+44
-6
lines changed

2 files changed

+44
-6
lines changed

packages/server/src/__tests__/test-helpers.ts

Lines changed: 16 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -25,23 +25,38 @@ export async function cleanupTestFiles(): Promise<void> {
2525
}
2626

2727
export function createMockElement(): TargetedElement {
28+
const text = 'Test Element';
2829
return {
2930
selector: 'div.test-element',
3031
tagName: 'DIV',
3132
id: 'test-id',
3233
classes: ['test-class'],
33-
innerText: 'Test Element',
34+
innerText: text,
35+
textContent: text,
36+
textDetail: 'full',
37+
textVariants: {
38+
visible: text,
39+
full: text,
40+
},
3441
attributes: { 'data-test': 'true' },
3542
position: {
3643
x: 100, y: 200, width: 300, height: 50,
3744
},
45+
cssLevel: 1,
3846
cssProperties: {
3947
display: 'block',
4048
position: 'relative',
4149
fontSize: '16px',
4250
color: 'rgb(0, 0, 0)',
4351
backgroundColor: 'rgb(255, 255, 255)',
4452
},
53+
cssComputed: {
54+
display: 'block',
55+
position: 'relative',
56+
fontSize: '16px',
57+
color: 'rgb(0, 0, 0)',
58+
backgroundColor: 'rgb(255, 255, 255)',
59+
},
4560
timestamp: Date.now(),
4661
url: 'https://example.com',
4762
tabId: 123,

packages/server/src/mcp-handler.ts

Lines changed: 28 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,14 @@ import {
55
ListToolsRequestSchema,
66
} from '@modelcontextprotocol/sdk/types.js';
77
import { version } from 'process';
8+
import { CSS_DETAIL_OPTIONS, TEXT_DETAIL_OPTIONS } from '@mcp-pointer/shared/detail';
89
import type PointerWebSocketServer from './websocket-server';
10+
import {
11+
normalizeDetailParameters,
12+
shapeElementForDetail,
13+
type DetailParameters,
14+
type NormalizedDetailParameters,
15+
} from './utils/element-detail';
916

1017
enum MCPToolName {
1118
GET_POINTED_ELEMENT = 'get-pointed-element',
@@ -47,10 +54,21 @@ export default class MCPHandler {
4754
tools: [
4855
{
4956
name: MCPToolName.GET_POINTED_ELEMENT,
50-
description: 'Get information about the currently pointed/shown DOM element from the browser extension, in order to let you see a specific element the user is showing you on his/her the browser.',
57+
description: 'Get information about the currently pointed/shown DOM element. Control returned payload size with optional textDetail (full|visible|none) and cssLevel (0-3).',
5158
inputSchema: {
5259
type: 'object',
53-
properties: {},
60+
properties: {
61+
textDetail: {
62+
type: 'string',
63+
enum: [...TEXT_DETAIL_OPTIONS],
64+
description: 'Controls how much text is returned. full (default) includes hidden text fallback, visible uses only rendered text, none omits text fields.',
65+
},
66+
cssLevel: {
67+
type: 'integer',
68+
enum: [...CSS_DETAIL_OPTIONS],
69+
description: 'Controls CSS payload detail. 0 omits CSS, 1 includes layout basics, 2 adds box model, 3 returns the full computed style.',
70+
},
71+
},
5472
required: [],
5573
},
5674
},
@@ -60,13 +78,16 @@ export default class MCPHandler {
6078

6179
private async handleCallTool(request: any) {
6280
if (request.params.name === MCPToolName.GET_POINTED_ELEMENT) {
63-
return this.getTargetedElement();
81+
const normalized = normalizeDetailParameters(
82+
request.params.arguments as DetailParameters | undefined,
83+
);
84+
return this.getTargetedElement(normalized);
6485
}
6586

6687
throw new Error(`Unknown tool: ${request.params.name}`);
6788
}
6889

69-
private getTargetedElement() {
90+
private getTargetedElement(details: NormalizedDetailParameters) {
7091
const element = this.wsServer.getCurrentElement();
7192

7293
if (!element) {
@@ -81,11 +102,13 @@ export default class MCPHandler {
81102
};
82103
}
83104

105+
const shapedElement = shapeElementForDetail(element, details.textDetail, details.cssLevel);
106+
84107
return {
85108
content: [
86109
{
87110
type: 'text',
88-
text: JSON.stringify(element, null, 2),
111+
text: JSON.stringify(shapedElement, null, 2),
89112
},
90113
],
91114
};

0 commit comments

Comments
 (0)