feat: add chrome_switch_tab

This commit is contained in:
wyhao31
2025-07-24 22:39:39 -07:00
parent 78e5c16a87
commit c71a968fb2
7 changed files with 114 additions and 19 deletions
+2 -7
View File
@@ -9,6 +9,7 @@
**📖 Documentation**: [English](README.md) | [中文](README_zh.md)
> The project is still in its early stages and is under intensive development. More features, stability improvements, and other enhancements will follow.
---
## 🎯 What is Chrome MCP Server?
@@ -144,6 +145,7 @@ Complete tool list: [Complete Tool List](docs/TOOLS.md)
- `get_windows_and_tabs` - List all browser windows and tabs
- `chrome_navigate` - Navigate to URLs and control viewport
- `chrome_switch_tab` - Switch the current active tab
- `chrome_close_tabs` - Close specific tabs or windows
- `chrome_go_back_or_forward` - Browser navigation control
- `chrome_inject_script` - Inject content scripts into web pages
@@ -214,7 +216,6 @@ prompt: [modify-web-prompt](prompt/modify-web.md)
Instruction: Help me modify the current page's style and remove advertisements.
https://youtu.be/twI6apRKHsk
https://github.com/user-attachments/assets/69cb561c-2e1e-4665-9411-4a3185f9643e
### AI automatically captures network requests for you
@@ -223,7 +224,6 @@ query: I want to know what the search API for Xiaohongshu is and what the respon
https://youtu.be/1hHKr7XKqnQ
https://github.com/user-attachments/assets/063f44ae-1754-46b6-b141-5988c86e4d96
### AI helps analyze your browsing history
@@ -232,7 +232,6 @@ query: Analyze my browsing history from the past month
https://youtu.be/jf2UZfrR2Vk
https://github.com/user-attachments/assets/e7a35118-e50e-4b1c-a790-0878aa2505ab
### Web page conversation
@@ -247,7 +246,6 @@ https://github.com/user-attachments/assets/08aa86aa-7706-4df2-b400-576e2c7fcc7f
query: Take a screenshot of Hugging Face's homepage
https://youtu.be/7ycK6iksWi4
https://github.com/user-attachments/assets/b081e41b-6309-40d6-885b-0da01691b12e
### AI automatically takes screenshots for you (element screenshots)
@@ -255,7 +253,6 @@ https://github.com/user-attachments/assets/b081e41b-6309-40d6-885b-0da01691b12e
query: Capture the icon from Hugging Face's homepage
https://youtu.be/ev8VivANIrk
https://github.com/user-attachments/assets/25657076-b84b-4459-a72f-90f896f06364
### AI helps manage bookmarks
@@ -264,7 +261,6 @@ query: Add the current page to bookmarks and put it in an appropriate folder
https://youtu.be/R_83arKmFTo
https://github.com/user-attachments/assets/73c1ea26-65fb-4b5e-b537-e32fa9bcfa52
### Automatically close web pages
@@ -273,7 +269,6 @@ query: Close all shadcn-related web pages
https://youtu.be/2wzUT6eNVg4
https://github.com/user-attachments/assets/ff160f48-58e0-4c76-a6b0-c4e1f91370c8
## 🤝 Contributing
+1 -11
View File
@@ -79,7 +79,6 @@ mcp-chrome-bridge register
- 启用"开发者模式"
- 点击"加载已解压的扩展程序",选择 `your/dowloaded/extension/folder`
- 点击插件图标打开插件,点击连接即可看到mcp的配置
<img width="475" alt="截屏2025-06-09 15 52 06" src="https://github.com/user-attachments/assets/241e57b8-c55f-41a4-9188-0367293dc5bc" />
### 在支持MCP协议的客户端中使用
@@ -146,6 +145,7 @@ pnpm list -g mcp-chrome-bridge
- `get_windows_and_tabs` - 列出所有浏览器窗口和标签页
- `chrome_navigate` - 导航到 URL 并控制视口
- `chrome_switch_tab` - 切换当前显示的标签页
- `chrome_close_tabs` - 关闭特定标签页或窗口
- `chrome_go_back_or_forward` - 浏览器导航控制
- `chrome_inject_script` - 向网页注入内容脚本
@@ -200,7 +200,6 @@ prompt: [excalidraw-prompt](prompt/excalidraw-prompt.md)
指令:帮我总结当前页面内容,然后画个图帮我理解
https://www.youtube.com/watch?v=3fBPdUBWVz0
https://github.com/user-attachments/assets/f14f79a6-9390-4821-8296-06d020bcfc07
### ai先分析图片的内容元素,然后再自动控制excalidraw把图片模仿出来
@@ -217,7 +216,6 @@ prompt: [modify-web-prompt](prompt/modify-web.md)
指令:帮我修改当前页面的样式,去掉广告
https://youtu.be/twI6apRKHsk
https://github.com/user-attachments/assets/aedbe98d-e90c-4a58-a4a5-d888f7293d8e
### ai自动帮你捕获网络请求
@@ -225,7 +223,6 @@ https://github.com/user-attachments/assets/aedbe98d-e90c-4a58-a4a5-d888f7293d8e
指令:我想知道小红书的搜索接口是哪个,响应体结构是什么样的
https://youtu.be/1hHKr7XKqnQ
https://github.com/user-attachments/assets/063f44ae-1754-46b6-b141-5988c86e4d96
### ai帮你分析你的浏览记录
@@ -233,7 +230,6 @@ https://github.com/user-attachments/assets/063f44ae-1754-46b6-b141-5988c86e4d96
指令:分析一下我近一个月的浏览记录
https://youtu.be/jf2UZfrR2Vk
https://github.com/user-attachments/assets/e7a35118-e50e-4b1c-a790-0878aa2505ab
### 网页对话
@@ -241,7 +237,6 @@ https://github.com/user-attachments/assets/e7a35118-e50e-4b1c-a790-0878aa2505ab
指令:翻译并总结当前网页
https://youtu.be/FlJKS9UQyC8
https://github.com/user-attachments/assets/08aa86aa-7706-4df2-b400-576e2c7fcc7f
### ai帮你自动截图(网页截图)
@@ -263,7 +258,6 @@ https://github.com/user-attachments/assets/25657076-b84b-4459-a72f-90f896f06364
指令:将当前页面添加到书签中,放到合适的文件夹
https://youtu.be/R_83arKmFTo
https://github.com/user-attachments/assets/73c1ea26-65fb-4b5e-b537-e32fa9bcfa52
### 自动关闭网页
@@ -271,7 +265,6 @@ https://github.com/user-attachments/assets/73c1ea26-65fb-4b5e-b537-e32fa9bcfa52
指令:关闭所有shadcn相关的网页
https://youtu.be/2wzUT6eNVg4
https://github.com/user-attachments/assets/ff160f48-58e0-4c76-a6b0-c4e1f91370c8
## 🤝 贡献指南
@@ -309,6 +302,3 @@ https://github.com/user-attachments/assets/ff160f48-58e0-4c76-a6b0-c4e1f91370c8
拉群的目的是让踩过坑的大佬们互相帮忙解答问题,因本人平时要忙着搬砖,不一定能及时解答
![IMG_6296](https://github.com/user-attachments/assets/ecd2e084-24d2-4038-b75f-3ab020b55594)
@@ -476,3 +476,58 @@ class GoBackOrForwardTool extends BaseBrowserToolExecutor {
}
export const goBackOrForwardTool = new GoBackOrForwardTool();
interface SwitchTabToolParams {
tabId: number;
windowId?: number;
}
/**
* Tool for switching the active tab
*/
class SwitchTabTool extends BaseBrowserToolExecutor {
name = TOOL_NAMES.BROWSER.SWITCH_TAB;
async execute(args: SwitchTabToolParams): Promise<ToolResult> {
const { tabId, windowId } = args;
console.log(`Attempting to switch to tab ID: ${tabId} in window ID: ${windowId}`);
try {
if (windowId !== undefined) {
await chrome.windows.update(windowId, { focused: true });
}
await chrome.tabs.update(tabId, { active: true });
const updatedTab = await chrome.tabs.get(tabId);
return {
content: [
{
type: 'text',
text: JSON.stringify({
success: true,
message: `Successfully switched to tab ID: ${tabId}`,
tabId: updatedTab.id,
windowId: updatedTab.windowId,
url: updatedTab.url,
}),
},
],
isError: false,
};
} catch (error) {
if (chrome.runtime.lastError) {
console.error(`Chrome API Error: ${chrome.runtime.lastError.message}`, error);
return createErrorResponse(`Chrome API Error: ${chrome.runtime.lastError.message}`);
} else {
console.error('Error in SwitchTabTool.execute:', error);
return createErrorResponse(
`Error switching tab: ${error instanceof Error ? error.message : String(error)}`,
);
}
}
}
}
export const switchTabTool = new SwitchTabTool();
@@ -1,4 +1,4 @@
export { navigateTool, closeTabsTool, goBackOrForwardTool } from './common';
export { navigateTool, closeTabsTool, goBackOrForwardTool, switchTabTool } from './common';
export { windowTool } from './window';
export { vectorSearchTabsContentTool as searchTabsContentTool } from './vector-search';
export { screenshotTool } from './screenshot';
+18
View File
@@ -82,6 +82,24 @@ Close specific tabs or windows.
}
```
### `chrome_switch_tab`
Switch to a specific browser tab.
**Parameters**:
- `tabId` (number, required): The ID of the tab to switch to.
- `windowId` (number, optional): The ID of the window where the tab is located.
**Example**:
```json
{
"tabId": 456,
"windowId": 123
}
```
### `chrome_go_back_or_forward`
Navigate browser history.
+18
View File
@@ -82,6 +82,24 @@
}
```
### `chrome_switch_tab`
切换到指定的浏览器标签页。
**参数**:
- `tabId` (数字,必需):要切换到的标签页的 ID。
- `windowId` (数字,可选):该标签页所在窗口的 ID。
**示例**:
```json
{
"tabId": 456,
"windowId": 123
}
```
### `chrome_go_back_or_forward`
浏览器历史导航。
+19
View File
@@ -7,6 +7,7 @@ export const TOOL_NAMES = {
NAVIGATE: 'chrome_navigate',
SCREENSHOT: 'chrome_screenshot',
CLOSE_TABS: 'chrome_close_tabs',
SWITCH_TAB: 'chrome_switch_tab',
GO_BACK_OR_FORWARD: 'chrome_go_back_or_forward',
WEB_FETCHER: 'chrome_get_web_content',
CLICK: 'chrome_click_element',
@@ -108,6 +109,24 @@ export const TOOL_SCHEMAS: Tool[] = [
required: [],
},
},
{
name: TOOL_NAMES.BROWSER.SWITCH_TAB,
description: 'Switch to a specific browser tab',
inputSchema: {
type: 'object',
properties: {
tabId: {
type: 'number',
description: 'The ID of the tab to switch to.',
},
windowId: {
type: 'number',
description: 'The ID of the window where the tab is located.',
},
},
required: ['tabId'],
},
},
{
name: TOOL_NAMES.BROWSER.GO_BACK_OR_FORWARD,
description: 'Navigate back or forward in browser history',