mcp module¶
Tools for Binary Ninja’s Model Context Protocol (MCP) server.
A tool registered here is offered by every MCP server in the process, alongside the built-in bn_*
tools. The simplest way to write one is the tool decorator, which builds the tool’s JSON
Schema from the function’s signature and docstring:
from binaryninja import mcp
@mcp.tool(read_only=True)
def myplugin_function_count(call: mcp.ToolCall, min_size: int = 0) -> dict:
"""Count the functions in the active binary view.
:param min_size: Only count functions with at least this many bytes.
"""
functions = [f for f in call.binary_view.functions if f.total_bytes >= min_size]
return {"count": len(functions)}
Class |
Description |
|---|---|
A parameter annotated |
|
Clamps an |
|
A parameter annotated |
|
Rejects an |
|
Rejects an |
|
Rejects an empty |
|
Evaluates an … |
|
Supplies the JSON Schema for a parameter the other annotations cannot describe. Use it as… |
|
A tool in the tool registry. Iterating over |
|
One invocation of a tool. Once the tool returns, |
|
Raised by a tool to return an error result with a machine-readable code. |
|
The result of a tool. A tool may instead return a |
Function |
Description |
|---|---|
Registers a tool for the life of the process. |
|
Registers the decorated function as a tool. The function’s first parameter receives the… |
Address¶
ClampTo¶
IntegerExpression¶
Maximum¶
Minimum¶
NonEmpty¶
RelativeTo¶
- class RelativeTo[source]¶
Bases:
objectEvaluates an
IntegerExpressionparameter’s expression with an earlierAddress,IntegerExpressionorintparameter’s value as$here. When that argument is absent, its default is used, or 0 when it has none. Use it asAnnotated[mcp.IntegerExpression, mcp.RelativeTo("address")], for example for a length measured from an address, or on the items of aListof them.
Schema¶
Tool¶
- class Tool[source]¶
Bases:
objectA tool in the tool registry. Iterating over
Toollists every registered tool, sorted by name.- invoke(arguments: dict | None = None, view: BinaryView | None = None) dict[source]¶
Runs the tool as an MCP server would, against
view, and returns the MCPCallToolResult.- Parameters:
arguments (dict | None)
view (BinaryView | None)
- Return type:
- property annotations: McpToolAnnotation¶
A combination of
McpToolAnnotationflags.
- property scope: McpToolScope¶
ToolCall¶
- class ToolCall[source]¶
Bases:
objectOne invocation of a tool. Once the tool returns,
binary_viewisNoneandis_cancelledisTrue.- parse_address(value: Any, here: int = 0) int[source]¶
Evaluates an address expression string, with
hereas the value of$here. RaisesValueErrorwhen it is invalid.
- parse_integer(value: Any, here: int = 0) int[source]¶
Evaluates an unsigned integer or an expression string, with
hereas the value of$here. RaisesValueErrorwhen it is invalid.
- property binary_view: BinaryView | None¶
The binary view the MCP session targets. Never
Nonewhile a BinaryView-scoped tool runs.
ToolError¶
ToolResult¶
- class ToolResult[source]¶
Bases:
objectThe result of a tool. A tool may instead return a
str(text), adict(structured content) orNone(an empty result).- add_warning(code: str, message: str) ToolResult[source]¶
Adds an advisory warning about the result, such as analysis that has not finished. Clients see it in the structured content’s reserved
warningsmember, and after any text.- Parameters:
- Return type:
- static from_error(error: ToolError) ToolResult[source]¶
- Parameters:
error (ToolError)
- Return type:
register_tool¶
- register_tool(name: str, description: str, input_schema: dict, handler: ~typing.Callable[[~binaryninja.mcp.ToolCall, dict], ~typing.Any], *, title: str = '', scope: ~binaryninja.enums.McpToolScope = McpToolScope.BinaryViewScope, annotations: ~binaryninja.enums.McpToolAnnotation = <McpToolAnnotation: 0>, output_schema: dict | None = None) Tool[source]¶
Registers a tool for the life of the process.
- Parameters:
handler (Callable[[ToolCall, dict], Any]) – Called as
handler(call, arguments)with the decoded arguments object. Returns astr,dict,ToolResultorNone, or raisesToolError.name (str)
description (str)
input_schema (dict)
title (str)
scope (McpToolScope)
annotations (McpToolAnnotation)
output_schema (dict | None)
- Raises:
ValueError – The definition is invalid or its name is already registered.
- Return type:
tool()¶
- tool(name: str | None = None, *, title: str = '', scope: McpToolScope = McpToolScope.BinaryViewScope, read_only: bool = False, destructive: bool = False, idempotent: bool = False, open_world: bool = False, output_schema: dict | None = None)[source]¶
Registers the decorated function as a tool. The function’s first parameter receives the
ToolCall. Every other parameter becomes a tool parameter, and needs a type annotation and a:param name:line in the docstring. The docstring’s leading paragraph becomes the tool’s description.Supported annotations are
str,int,float,bool,Address,IntegerExpression, Binary Ninja enums (by member name),Literalof strings,List[T],Annotated[T, Schema({...})],Annotated[IntegerExpression, RelativeTo("name")],Annotated[str, NonEmpty()]andintannotated withMinimum,MaximumorClampTo.Optional[T],T | Noneor a default value makes a parameter optional, and a null argument for one is treated as absent.Use it with parentheses or without, as
@mcp.tool()or@mcp.tool.The tool’s name defaults to the function’s name. Arguments are checked before the function runs, and a missing, invalid or undeclared argument produces an
invalid_paramserror without calling it.