Skip to main content

FastAPI-style framework for building composable Function Apps for Cognite Functions with automatic OpenAPI schema generation and MCP integration

Project description

Function Apps

⚠️ IMPORTANT: This project has been renamed

Previously known as Cognite Typed Functions (cognite-typed-functions)

What changed:

  • Package name: cognite-typed-functionscognite-function-apps
  • Import path: from cognite_typed_functionsfrom cognite_function_apps
  • CLI command: ctffun

For existing users: Update your dependencies and change all imports. All class names and APIs remain unchanged.

Enterprise-grade framework for building composable Function Apps for Cognite Functions with automatic validation, built-in introspection, and AI integration.

Why Function Apps?

Standard Cognite Functions require a simple handle(client, data) function, which becomes unwieldy for complex APIs. This framework provides composable architecture, automatic validation, and built-in introspection.

Standard Cognite Function:

def handle(client, data):
    try:
        asset_no = int(data["assetNo"])  # Manual validation
        include_tax = data.get("includeTax", "false").lower() == "true"  # Manual parsing
        # Handle routing manually based on data
        if data.get("action") == "get_item":
            # Implementation here
        elif data.get("action") == "create_item":
            # Different implementation
    except Exception as e:
        return {"error": str(e)}  # Basic error handling

With Function Apps:

@app.get("/items/{item_id}")
def get_item(client: CogniteClient, item_id: int, include_tax: bool = False) -> ItemResponse:
    """Retrieve an item by ID"""
    # Type validation and coercion handled automatically
    # Clear function signature with proper types
    # Automatic error handling and response formatting

Key Features

  • Type-safe routing - Decorator-based syntax with automatic validation
  • Async/await support - Write both sync and async handlers
  • Error handling - Comprehensive error handling with structured responses
  • Logging - Enterprise logging with dependency injection
  • Distributed tracing - OpenTelemetry-based tracing
  • Dependency injection - Inject custom dependencies
  • Introspection - Built-in schema, routes, health endpoints
  • Model Context Protocol - Native AI tool exposure
  • Local development server - Test locally with interactive API docs
  • Function client - Notebook-first client for exploring functions

Installation

Requirements:

  • Python 3.10 or higher
  • uv (recommended) or pip
# Install the package
pip install cognite-function-apps

# Optional: Install with CLI support for dev server
pip install cognite-function-apps[cli]

# Optional: Install with tracing support
pip install cognite-function-apps[tracing]

Quick Start

from cognite.client import CogniteClient
from pydantic import BaseModel
from cognite_function_apps import FunctionApp, create_function_service

# Create your app
app = FunctionApp(title="My API", version="1.0.0")

# Define your models
class Item(BaseModel):
    name: str
    description: str | None = None
    price: float
    tax: float | None = None

class ItemResponse(BaseModel):
    id: int
    item: Item
    total_price: float

# Define your endpoints
@app.get("/items/{item_id}")
def get_item(
    client: CogniteClient,
    item_id: int,
    include_tax: bool = False
) -> ItemResponse:
    """Retrieve an item by ID"""
    item = Item(
        name=f"Item {item_id}",
        price=100.0,
        tax=10.0 if include_tax else None
    )
    total = item.price + (item.tax or 0)
    return ItemResponse(id=item_id, item=item, total_price=total)

@app.post("/items/")
def create_item(client: CogniteClient, item: Item) -> ItemResponse:
    """Create a new item"""
    new_id = 12345  # Your creation logic here
    total = item.price + (item.tax or 0)
    return ItemResponse(id=new_id, item=item, total_price=total)

# Export the handler for Cognite Functions
handle = create_function_service(app)

Test Locally

Install with CLI support and run the development server:

pip install cognite-function-apps[cli]
fun serve examples/

Visit http://localhost:8000/docs for interactive API documentation.

See the Local Development Server guide for detailed setup instructions.

Examples

The framework includes a complete example in examples/handler.py demonstrating:

  • Type-safe routing with decorator syntax
  • MCP integration for AI tool exposure
  • Built-in introspection endpoints
  • Async handler support
  • Composable app architecture

License

This project is licensed under the Apache License 2.0 - see the LICENSE file for details.

Acknowledgments

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

cognite_function_apps-0.11.0.tar.gz (384.8 kB view details)

Uploaded Source

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

cognite_function_apps-0.11.0-py3-none-any.whl (108.5 kB view details)

Uploaded Python 3

File details

Details for the file cognite_function_apps-0.11.0.tar.gz.

File metadata

  • Download URL: cognite_function_apps-0.11.0.tar.gz
  • Upload date:
  • Size: 384.8 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: uv/0.11.0 {"installer":{"name":"uv","version":"0.11.0","subcommand":["publish"]},"python":null,"implementation":{"name":null,"version":null},"distro":{"name":"Ubuntu","version":"24.04","id":"noble","libc":null},"system":{"name":null,"release":null},"cpu":null,"openssl_version":null,"setuptools_version":null,"rustc_version":null,"ci":true}

File hashes

Hashes for cognite_function_apps-0.11.0.tar.gz
Algorithm Hash digest
SHA256 15f8cec8ab3ce34c2f208dcc4200073c7e2a7cfa52553e942a97dee8aa99e71d
MD5 9aa08e84af3dd26ae71aa0f29a279e5d
BLAKE2b-256 cbe8de03552c517b3ab86ddb83ea207339c04ede42208b03ccdcde3f49964455

See more details on using hashes here.

File details

Details for the file cognite_function_apps-0.11.0-py3-none-any.whl.

File metadata

  • Download URL: cognite_function_apps-0.11.0-py3-none-any.whl
  • Upload date:
  • Size: 108.5 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: uv/0.11.0 {"installer":{"name":"uv","version":"0.11.0","subcommand":["publish"]},"python":null,"implementation":{"name":null,"version":null},"distro":{"name":"Ubuntu","version":"24.04","id":"noble","libc":null},"system":{"name":null,"release":null},"cpu":null,"openssl_version":null,"setuptools_version":null,"rustc_version":null,"ci":true}

File hashes

Hashes for cognite_function_apps-0.11.0-py3-none-any.whl
Algorithm Hash digest
SHA256 b9b8681fab894c81e333d0b8f15f68c063d14ad80cd7e6910dbbf7bfb7727719
MD5 64ce75d772c67ca1efa7afd17a589103
BLAKE2b-256 db9f1fd69cb0dae2ff8dd040ab2a81926faf5ceb6bde864a2361f6f89ebc68b0

See more details on using hashes here.

Supported by

AWS Cloud computing and Security Sponsor Datadog Monitoring Depot Continuous Integration Fastly CDN Google Download Analytics Pingdom Monitoring Sentry Error logging StatusPage Status page