mcp sse 直接调用mcp方法
haris-musa/excel-mcp-server | DeepWiki
from mcp import ClientSession
from mcp.client.sse import sse_client
import asyncio async def connect_to_mcp_sse(): """通过 SSE 连接到 MCP 服务器""" url = "http://localhost:8017/sse" async with sse_client(url) as (read, write): async with ClientSession(read, write) as session: # 初始化连接 await session.initialize() print("已连接到 MCP 服务器") # 列出所有可用工具 tools = await session.list_tools() print("\n可用工具:") for tool in tools.tools: print(f"- {tool.name}: {tool.description}") # 调用工具示例 result = await session.call_tool( "create_workbook", arguments={"filepath": "test.xlsx"} ) print(f"\n工具调用结果: {result}") # 读取数据示例 result = await session.call_tool( "read_data_from_excel", arguments={ "filepath": "test.xlsx", "sheet_name": "Sheet1", "start_cell": "A1", "preview_only": True } ) print(f"\n读取数据结果: {result}") # 运行客户端
asyncio.run(connect_to_mcp_sse())
已连接到 MCP 服务器可用工具:
- apply_formula:Apply Excel formula to cell.Excel formula will write to cell with verification.- validate_formula_syntax: Validate Excel formula syntax without applying it.
- format_range: Apply formatting to a range of cells.
- read_data_from_excel:Read data from Excel worksheet with cell metadata including validation rules.Args:filepath: Path to Excel filesheet_name: Name of worksheetstart_cell: Starting cell (default A1)end_cell: Ending cell (optional, auto-expands if not provided)preview_only: Whether to return preview onlyReturns:JSON string containing structured cell data with validation metadata.Each cell includes: address, value, row, column, and validation info (if any).- write_data_to_excel:Write data to Excel worksheet.Excel formula will write to cell without any verification.PARAMETERS:filepath: Path to Excel filesheet_name: Name of worksheet to write todata: List of lists containing data to write to the worksheet, sublists are assumed to be
rowsstart_cell: Cell to start writing to, default is "A1"- create_workbook: Create new Excel workbook.
- create_worksheet: Create new worksheet in workbook.
- create_chart: Create chart in worksheet.
- create_pivot_table: Create pivot table in worksheet.
- create_table: Creates a native Excel table from a specified range of data.
- copy_worksheet: Copy worksheet within workbook.
- delete_worksheet: Delete worksheet from workbook.
- rename_worksheet: Rename worksheet in workbook.
- get_workbook_metadata: Get metadata about workbook including sheets, ranges, etc.
- merge_cells: Merge a range of cells.
- unmerge_cells: Unmerge a range of cells.
- get_merged_cells: Get merged cells in a worksheet.
- copy_range: Copy a range of cells to another location.
- delete_range: Delete a range of cells and shift remaining cells.
- validate_excel_range: Validate if a range exists and is properly formatted.
- get_data_validation_info:Get all data validation rules in a worksheet.This tool helps identify which cell ranges have validation rulesand what types of validation are applied.Args:filepath: Path to Excel filesheet_name: Name of worksheetReturns:JSON string containing all validation rules in the worksheet- insert_rows: Insert one or more rows starting at the specified row.
- insert_columns: Insert one or more columns starting at the specified column.
- delete_sheet_rows: Delete one or more rows starting at the specified row.
- delete_sheet_columns: Delete one or more columns starting at the specified column.工具调用结果: meta=None content=[TextContent(type='text', text='Created workbook at ./excel_fi
les\\test.xlsx', annotations=None, meta=None)] structuredContent={'result': 'Created workbook
at ./excel_files\\test.xlsx'} isError=False读取数据结果: meta=None content=[TextContent(type='text', text='{\n "range": "A1:A1",\n "she
et_name": "Sheet1",\n "cells": [\n {\n "address": "A1",\n "value": null,\n
"row": 1,\n "column": 1,\n "validation": {\n "has_validation": false\n }
\n }\n ]\n}', annotations=None, meta=None)] structuredContent={'result': '{\n "range": "A
1:A1",\n "sheet_name": "Sheet1",\n "cells": [\n {\n "address": "A1",\n "value":null,\n "row": 1,\n "column": 1,\n