get_keyword_stats
Retrieve and analyze keyword statistics for a specific query, including data by country and language, to enhance SEO strategies using Bing Webmaster Tools.
Instructions
Retrieve keyword statistics for a specific query.
Args: query: The keyword query country: The country code (i.e. gb) language: The language and country code (i.e. en-GB)
Returns: List[KeywordStats]: List of keyword statistics
Raises: BingWebmasterError: If statistics cannot be retrieved
Input Schema
TableJSON Schema
| Name | Required | Description | Default |
|---|---|---|---|
| country | Yes | ||
| language | Yes | ||
| query | Yes | ||
| self | Yes |
Implementation Reference
- mcp_server_bwt/tools/bing_webmaster.py:152-154 (registration)Registers the 'get_keyword_stats' MCP tool by wrapping the underlying keyword_analysis.KeywordAnalysisService.get_keyword_stats method. The wrapper is decorated as @mcp.tool() and preserves the original signature and docstring.get_keyword_stats = wrap_service_method( # noqa: F841 mcp, service, "keywords", "get_keyword_stats" )
- The wrap_service_method function creates the actual handler for the tool. It generates a wrapper function decorated with @mcp.tool(), which executes the underlying service method asynchronously within the service context manager.def wrap_service_method( mcp: FastMCP, service: BingWebmasterService, service_attr: str, method_name: str ) -> Callable[..., Any]: """Helper function to wrap a service method with mcp.tool() while preserving its signature and docstring. Args: mcp: The MCP server instance service: The BingWebmasterService instance service_attr: The service attribute name (e.g., 'sites', 'submission') method_name: The method name to wrap Returns: The wrapped method as an MCP tool """ # Get the service class from our mapping service_class = SERVICE_CLASSES[service_attr] # Get the original method original_method = getattr(service_class, method_name) # Get the signature sig = inspect.signature(original_method) # Remove 'self' parameter from signature parameters = list(sig.parameters.values())[1:] # Skip 'self' # Create new signature without 'self' new_sig = sig.replace(parameters=parameters) # Create wrapper function with same signature @mcp.tool() @wraps(original_method) async def wrapper(*args: Any, **kwargs: Any) -> Any: # Filter out any 'self' arguments that might be passed by the MCP client kwargs = {k: v for k, v in kwargs.items() if k != "self"} async with service as s: service_obj = getattr(s, service_attr) # Get the method from the instance method = getattr(service_obj, method_name) # Call the method directly - it's already bound to the instance return await method(*args, **kwargs) # Copy signature and docstring wrapper.__signature__ = new_sig # type: ignore wrapper.__doc__ = original_method.__doc__ return wrapper
- Instantiates the 'keywords' service attribute on BingWebmasterService, which provides the get_keyword_stats method implementation (from external library).self.keywords = keyword_analysis.KeywordAnalysisService(self.client)
- Maps the 'keywords' service attribute to its class in SERVICE_CLASSES, used by wrap_service_method to get the original method signature."keywords": keyword_analysis.KeywordAnalysisService,
- mcp_server_bwt/main.py:17-17 (registration)Calls add_bing_webmaster_tools which performs all tool registrations, including get_keyword_stats.add_bing_webmaster_tools(mcp, bing_service)