Skip to main content
Glama
piekstra

New Relic MCP Server

by piekstra

create_log_parsing_rule

Define custom parsing rules to extract structured data from log entries, enabling targeted analysis and monitoring of application logs.

Instructions

Create a new log parsing rule

Input Schema

TableJSON Schema
NameRequiredDescriptionDefault
descriptionYes
grokYes
nrqlYes
enabledNo
luceneNo
account_idNo

Implementation Reference

  • MCP tool handler function decorated with @mcp.tool(). Handles input parameters, client initialization check, delegates to log_parsing helper, and formats response as JSON.
    @mcp.tool()
    async def create_log_parsing_rule(
        description: str,
        grok: str,
        nrql: str,
        enabled: bool = True,
        lucene: str = "",
        account_id: Optional[str] = None,
    ) -> str:
        """Create a new log parsing rule"""
        if not client:
            return json.dumps({"error": "New Relic client not initialized"})
    
        acct_id = account_id or client.account_id
        if not acct_id:
            return json.dumps({"error": "Account ID required but not provided"})
    
        try:
            result = await log_parsing.create_log_parsing_rule(
                client, acct_id, description, grok, nrql, enabled, lucene
            )
            return json.dumps(result, indent=2)
        except Exception as e:
            return json.dumps({"error": str(e)}, indent=2)
  • Helper function containing the core logic: constructs and executes NerdGraph GraphQL mutation to create the log parsing rule in New Relic.
    async def create_log_parsing_rule(
        client,
        account_id: str,
        description: str,
        grok: str,
        nrql: str,
        enabled: bool = True,
        lucene: str = "",
    ) -> Dict[str, Any]:
        """Create a new log parsing rule"""
        # Escape special characters in GROK pattern for GraphQL
        # grok_escaped = grok.replace("\\", "\\\\")
    
        mutation = f"""
        mutation {{
            logConfigurationsCreateParsingRule(
                accountId: {int(account_id)},
                rule: {{
                    description: "{description}"
                    enabled: {str(enabled).lower()}
                    grok: "{grok}"
                    lucene: "{lucene}"
                    nrql: "{nrql}"
                }}
            ) {{
                rule {{
                    id
                    description
                    enabled
                    grok
                    lucene
                    nrql
                    updatedAt
                }}
                errors {{
                    message
                    type
                }}
            }}
        }}
        """
    
        result = await client.nerdgraph_query(mutation)
    
        if result and "data" in result:
            create_result = result["data"].get("logConfigurationsCreateParsingRule", {})
            if create_result.get("errors"):
                raise Exception(f"Failed to create rule: {create_result['errors']}")
            return create_result.get("rule", {})
    
        raise Exception("Failed to create parsing rule")

Latest Blog Posts

MCP directory API

We provide all the information about MCP servers via our MCP API.

curl -X GET 'https://glama.ai/api/mcp/v1/servers/piekstra/newrelic-mcp-server'

If you have feedback or need assistance with the MCP directory API, please join our Discord server