Skip to content

Tool utils

annotation_to_type_str(annotation)

Used to convert inspect annotations to type strings

Parameters:

Name Type Description Default
annotation

Inspect annotation

required

Returns:

Type Description
str

Type string

Source code in blue/utils/tool_utils.py
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
def annotation_to_type_str(annotation):
    """Used to convert inspect annotations to type strings

    Parameters:
        annotation: Inspect annotation

    Returns:
        (str): Type string
    """
    if type(annotation) == type:
        if annotation == inspect._empty:
            return "unknown"
        else:
            return annotation.__name__
    else:
        type_str = str(annotation)
        type_str = type_str.replace("typing.", "")
        return type_str

convert_type_string_to_mcp(type_str)

Converts a type string to MCP format.

Parameters:

Name Type Description Default
type_str str

Type string to convert

required

Returns:

Type Description
dict

MCP format dictionary

Source code in blue/utils/tool_utils.py
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
def convert_type_string_to_mcp(type_str):
    """Converts a type string to MCP format.

    Parameters:
        type_str (str): Type string to convert

    Returns:
        (dict): MCP format dictionary
    """
    if type_str == "":
        return {"type": "unknown"}
    if type_str.lower() == "int" or type_str.lower() == "float":
        return {"type": "number"}
    elif type_str.lower() == "str":
        return {"type": "string"}
    elif type_str.lower().find("list") == 0:
        return {"type": "array", "items": convert_type_string_to_mcp(type_str[len("List") + 1 : -1])}
    elif type_str.lower().find("dict") == 0:
        return {"type": "object", "properties": {}}
    else:
        return {"type": "unknown"}

extract_signature(f, mcp_format=False)

Extracts the signature of a function and returns it as a dictionary.

Parameters:

Name Type Description Default
f callable

Function to extract signature from

required
mcp_format bool

If True, converts types to MCP format. Defaults to False.

False

Returns:

Type Description
dict

Dictionary containing function parameters and return type.

Source code in blue/utils/tool_utils.py
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
def extract_signature(f, mcp_format=False):
    """Extracts the signature of a function and returns it as a dictionary.

    Parameters:
        f (callable): Function to extract signature from
        mcp_format (bool): If True, converts types to MCP format. Defaults to False.

    Returns:
        (dict): Dictionary containing function parameters and return type.
    """
    signature = inspect.signature(f)
    inspection = {}
    inspection["parameters"] = {}
    ps = signature.parameters.items()
    for pi in ps:
        k, p = pi
        ki = {}
        ki['type'] = annotation_to_type_str(p.annotation)
        if mcp_format:
            ki = json_utils.merge_json(ki, convert_type_string_to_mcp(ki['type']))
        default = p.default
        if default == inspect._empty:
            default = None
        if default:
            ki['required'] = False
            ki['default'] = default
        else:
            ki['required'] = True
        inspection["parameters"][k] = ki
    ri = signature.return_annotation
    r = {}
    if ri is not None:
        r['type'] = annotation_to_type_str(ri)
        if mcp_format:
            r = json_utils.merge_json(r, convert_type_string_to_mcp(r['type']))
    inspection["returns"] = r
    return inspection
Last update: 2025-10-05