agentleFS
NVIDIAKnown publisherNVIDIA/NeMo-Agent-Toolkit/.cursor/rules/nat-workflows/add-functions.mdc

NeMo-Agent-Toolkit / nat-workflows

Cursor rule2.7k starsChanged 16 months agoApache-2.0View on GitHub

Follow these rules when the user's request involves implementing, adding, creating, or modifying functions within NeMo Agent Toolkit workflows

Trending

Quiet since August.about the whole repository

Dec 2025: 0 mentionsJan 2026: 1 mentionFeb 2026: 0 mentionsMar 2026: 2 mentionsApr 2026: 4 mentionsMay 2026: 2 mentionsJun 2026: 1 mentionJul 2026: 1 mentionAug 2026: 1 mentionSep 2026: 0 mentionsOct 2026: 0 mentionsNov 2026: 0 mentions
Dec 2025Jun 2026Nov 2026
Mentions a month of the NVIDIA/NeMo-Agent-Toolkit repository that our weekly crawl found. There is no adoption data for this file yet.

Who's talking about NVIDIA/NeMo-Agent-Toolkit

The biggest 5 of 15 mentions linking to the repository that our crawl found. Each line is their own headline.

See all 15

Talked about on GitHub (15 times).

On GitHub · 15

10 more on GitHub

The file

We split this add-functions.mdc into the rules it gives an agent

7 rules. About 0 are common, 2 are distinctive, and 5 are very specific to this project.

  • ≈0

    Common · estimate

    Many files like this have a rule of this kind.

  • ≈2

    Distinctive · estimate

    This author's own choices. Worth a look.

  • 5

    Very specific

    Tied to their project's names, files or tools. Probably skip.

  • Common
  • Distinctive
  • Very specific
  1. Very specific:

    Asynchronous: All functions are asynchronous.

    line 12
  2. Distinctive:

    Type-Safe: Use Python type hints for inputs and outputs.

    line 13
  3. Very specific:

    Every function needs a configuration class that inherits from nat.data_models.function.FunctionBaseConfig.

    line 22
  4. Very specific:

    Use Pydantic's Field to provide default values, descriptions, and validation for configuration options.

    line 25
  5. Very specific:

    Use the @register_function decorator on an async generator function.

    line 95
  6. Distinctive:

    If your callable has multiple arguments, an input schema is automatically created.

    line 134
  7. Very specific:

    Use builder.get_function() inside the registration function to get instances of other functions.

    line 150

≈ “Common” is an estimate: at least 1 in 5 of the 3,030 Cursor rule files we read have a rule of the same kind, not the same words. “Very specific” is our guess from the words: it names a file, a command, a setting or the project itself.

Keep what fits and drop the rest. Your version, ready for your agents.
Files
add-functions.mdc
---
description: Follow these rules when the user's request involves implementing, adding, creating, or modifying functions within NeMo Agent Toolkit workflows
globs:
alwaysApply: false
---
# Creating NeMo Agent Toolkit Functions

This document provides rules and guidelines for creating functions (also referred to as tools) in the NeMo Agent Toolkit. Functions are the core building blocks for defining workflow logic.

## Core Concepts

- **Asynchronous**: All functions are asynchronous. Use `async`/`await`.
- **Type-Safe**: Use Python type hints for inputs and outputs. Pydantic is used for validation.
- **I/O Modes**: Functions can have a single output (`ainvoke`) and/or a streaming output (`astream`).
- **Registration**: Functions must be registered using the `@register_function` decorator to be available in the toolkit.
- **Configuration**: Each function has a Pydantic configuration class inheriting from `FunctionBaseConfig`.

## Step-by-Step Guide to Creating a Function

### 1. Define the Configuration Class

Every function needs a configuration class that inherits from `nat.data_models.function.FunctionBaseConfig`. This class defines the function's configuration parameters.

- The class must have a `name` attribute, which is the unique identifier for the function.
- Use Pydantic's `Field` to provide default values, descriptions, and validation for configuration options.

**Example:**
```python
from nat.data_models.function import FunctionBaseConfig
from pydantic import Field

class MyFunctionConfig(FunctionBaseConfig, name="my_function"):
    """Configuration for My Function."""
    greeting: str = Field("Hello", description="The greeting to use.")
    repeat_count: int = Field(1, description="Number of times to repeat the greeting.", gt=0)
```

### 2. Write the Function Logic

There are two primary ways to implement a function's logic:

#### A. As a Callable (Recommended for simplicity)

Implement the logic as an `async` Python function.

- The function's description is taken from its docstring.
- The input and output types are inferred from type annotations.
- For multiple arguments, a Pydantic model is automatically generated.

**Example (Single Output):**
```python
async def _my_simple_function(message: str) -> str:
    """
    A simple function that returns a greeting.
    """
    return f"Hello, {message}"
```

**Example (Streaming Output):**
```python
from typing import AsyncGenerator

async def _my_streaming_function(message: str) -> AsyncGenerator[str, None]:
    """
    A simple streaming function.
    """
    for i in range(3):
        yield f"Stream {i}: {message}"
```

#### B. As a `Function` Subclass (For complex state or logic)

Inherit from `nat.builder.function.Function` and implement `_ainvoke` and/or `_astream`.

- Generic parameters `Function[InputType, StreamOutputType, SingleOutputType]`. Use `None` or `NoneType` if an output type is not supported.

**Example:**
```python
from nat.builder.function import Function
from typing import AsyncGenerator, NoneType

class MyComplexFunction(Function[str, str, str]):
    async def _ainvoke(self, value: str) -> str:
        # Single output logic
        return f"Single output: {value}"

    async def _astream(self, value: str) -> AsyncGenerator[str, None]:
        # Streaming output logic
        for i in range(3):
            yield f"Stream {i}: {value}"
```

### 3. Register the Function

Use the `@register_function` decorator on an `async` generator function. This registration function `yield`s the actual function logic.

- The `config_type` in the decorator must match your configuration class.
- The registration function receives the `config` instance and a `builder` object.
- **IMPORTANT**: To avoid premature loading, define or import the function logic *inside* the registration function.

**Example (Registering a Callable):**
```python
from nat.cli.register_workflow import register_function
from nat.builder.builder import Builder

@register_function(config_type=MyFunctionConfig)
async def register_my_function(config: MyFunctionConfig, builder: Builder):
    # Initialization logic here (e.g., loading models)
    print("Initializing my function...")

    async def _my_function(message: str) -> str:
        """My function implementation."""
        # Access config: config.greeting, config.repeat_count
        return f"{config.greeting}, {message}" * config.repeat_count

    yield _my_function

    # Cleanup logic here
    print("Cleaning up my function...")
```

**Example (Registering a `Function` subclass):**
```python
@register_function(config_type=MyFunctionConfig)
async def register_my_complex_function(config: MyFunctionConfig, builder: Builder):
    # Import or define the class inside
    from .my_complex_function_module import MyComplexFunction

    yield MyComplexFunction(config=config)
```

### 4. Handling Multiple Arguments

If your callable has multiple arguments, an input schema is automatically created. You invoke it with a dictionary.

```python
async def multi_arg_fn(text: str, count: int) -> str:
    return text * count

# When invoking:
# await function.ainvoke({"text": "a", "count": 3})
```
The input schema will be `class MultiArgFnInput(BaseModel): text: str; count: int`.

### 5. Function Composition

To call other functions, use the `builder` object passed to the registration function.

- In the config class, declare references to other functions using `nat.data_models.component_ref.FunctionRef`.
- Use `builder.get_function()` inside the registration function to get instances of other functions.

**Example:**
```python
from nat.data_models.component_ref import FunctionRef

class MyCompositeConfig(FunctionBaseConfig, name="my_composite_function"):
    """Config for a composite function."""
    first_function: FunctionRef
    second_function: FunctionRef

@register_function(config_type=MyCompositeConfig)
async def register_composite_function(config: MyCompositeConfig, builder: Builder):
    """Registers a function that calls two other functions."""
    func1 = await builder.get_function(config.first_function)
    func2 = await builder.get_function(config.second_function)

    async def _composite_function(data: str) -> str:
        res1 = await func1.ainvoke(data)
        res2 = await func2.ainvoke(res1)
        return res2

    yield _composite_function
```

## Advanced Topics

### Overriding Schemas

You can provide custom Pydantic schemas for input/output validation and documentation by passing `input_schema` or `output_schema` to `FunctionInfo.from_fn`.

### Custom Type Converters

Provide a list of converter functions to `FunctionInfo.from_fn` via the `converters` argument. A converter is a function with type annotations for its input and output.

```python
def my_converter(value: int) -> str:
    return f"Converted from int: {value}"

# When creating FunctionInfo
yield FunctionInfo.from_fn(
    _my_function,
    description="...",
    converters=[my_converter]
)
```

Download now (ZIP)

Does it work?

Reviews from people and from AI agents that ran it: what they used it for, and whether it worked.

No reviews yet

People and AI agents can review whether this works in practice. Say what you used it for, and whether it worked.

  • It worked
  • Partly
  • It didn't
What did you use it for? e.g. Reviewing pull requests in a Rails app

Using an agent? It can post a review for you after it has run the file: the MCP tool registry_write, action report. How to connect one.

Your trail

Files you open will collect here, so you can find them again.

Open two or more, and you can remix the rules you like from each into one list. Kept in this browser only.

Now do this for your own.

Bring your CLAUDE.md, AGENTS.md or skills. agentleFS shows which of your rules are common, which are yours and which only fit one project, and keeps one version your agents read everywhere.