tinystruct Development Patterns
Architecture and implementation patterns for building modules with the tinystruct Java framework – a lightweight, high-performance framework that treats CLI and HTTP as equal citizens, requiring no main() method and minimal configuration.
Core Principle
CLI and HTTP are equal citizens. Every method annotated with @Action should ideally be runnable from both a terminal and a web browser without modification. This "dual-mode" capability is the core design philosophy of tinystruct.
When to Activate
When to Use
- Creating new
Applicationmodules by extendingAbstractApplication. - Defining routes and command-line actions using
@Action. - Handling per-request state via
Context. - Performing JSON serialization using the native
BuilderandBuilderscomponents. - Working with database persistence via
AbstractDataPOJOs. - Generating POJOs from database tables using the
generatecommand. - Implementing Server-Sent Events (SSE) for real-time push.
- Handling file uploads via multipart data.
- Making outbound HTTP requests with
URLRequestandHTTPHandler. - Configuring database connections or system settings in
application.properties. - Debugging routing conflicts (Actions) or CLI argument parsing.
How It Works
The tinystruct framework treats any method annotated with @Action as a routable endpoint for both terminal and web environments. Applications are created by extending AbstractApplication, which provides core lifecycle hooks like init() and access to the request Context.
Routing is handled by the ActionRegistry, which automatically maps path segments to method arguments and injects dependencies. For data-only services, the native Builder and Builders components should be used for JSON serialization to maintain a zero-dependency footprint. The database layer uses AbstractData POJOs paired with XML mapping files for CRUD operations without external ORM libraries.
Examples
Basic Application (MyService)
HTTP Mode Disambiguation (login)
Native JSON Data Handling (Builder + Builders)
SSE (Server-Sent Events)
File Upload
MCP Server and Tools Integration
tinystruct provides native support for the Model Context Protocol (MCP) starting with SDK version 1.7.26.
The MCP APIs (e.g., org.tinystruct.mcp.MCPTool, org.tinystruct.mcp.MCPServer, org.tinystruct.mcp.MCPException) are included directly in the core dependency:
SECURITY WARNING (Prompt Injection): Tool return values are fed directly back into the AI model's context window. You MUST validate and sanitize all caller-supplied arguments before including them in the tool's return string. Failure to sanitize inputs can allow an attacker to inject adversarial instructions (Prompt Injection) that override the model's behavior. Always validate length, character sets, and nullity.
To create an MCP Tool:
- Extend
org.tinystruct.mcp.MCPTool. - Annotate operations with
@Actionand declare parameters using@Argumentwithin theargumentsarray. - Accept parameters as explicit method arguments matching the keys in
@Argument. (Do not usegetContext().getAttribute(...)for tool arguments).
To deploy an MCP Server:
- Extend
org.tinystruct.mcp.MCPServer. - Override
init()and register your tools usingthis.registerTool(). The framework automatically scans and maps the@Actionmethods.
Run the server via the dispatcher:
Configuration
Settings are managed in src/main/resources/application.properties.
Access config values in your application:
Red Flags & Anti-patterns
Best Practices
- Granular Applications: Break logic into smaller, focused applications rather than one monolithic class.
- Setup in
init(): Leverageinit()for setup (config, DB) rather than the constructor. Do NOT callsetAction()— use@Actionannotation. - Mode Awareness: Use the
Modeparameter in@Actionto restrict sensitive operations toCLIonly or specific HTTP methods. - Context over Params: For optional CLI flags, use
getContext().getAttribute("--flag")rather than adding parameters to the method signature. - Asynchronous Events: For heavy tasks triggered by events, use
CompletableFuture.runAsync()inside the event handler.
Technical Reference
Detailed guides are available in the references/ directory:
- Architecture & Config [blocked] — Abstractions, Package Map, Properties
- Routing & @Action [blocked] — Annotation details, Modes, Parameters
- Data Handling [blocked] — Builder, Builders, JSON serialization & parsing
- Database Persistence [blocked] — AbstractData POJOs, CRUD, mapping XML, POJO generation
- System & Usage [blocked] — Context, Sessions, SSE, File Uploads, Events, Networking
- Testing Patterns [blocked] — JUnit 5 unit and HTTP integration testing
Reference Source Files (Internal)
src/main/java/org/tinystruct/AbstractApplication.java— Core base class with lifecycle hookssrc/main/java/org/tinystruct/system/annotation/Action.java— Annotation & Modessrc/main/java/org/tinystruct/application/ActionRegistry.java— Routing Enginesrc/main/java/org/tinystruct/data/component/Builder.java— JSON object serializersrc/main/java/org/tinystruct/data/component/Builders.java— JSON array serializersrc/main/java/org/tinystruct/data/component/AbstractData.java— Base POJO class with CRUDsrc/main/java/org/tinystruct/data/Mapping.java— Mapping XML parsersrc/main/java/org/tinystruct/data/tools/MySQLGenerator.java— POJO generator referencesrc/main/java/org/tinystruct/data/component/FieldType.java— SQL-to-Java type mappingssrc/main/java/org/tinystruct/data/component/Condition.java— Fluent SQL query buildersrc/main/java/org/tinystruct/http/SSEPushManager.java— SSE connection managementsrc/test/java/org/tinystruct/application/ActionRegistryTest.java— Registry test examplessrc/test/java/org/tinystruct/system/HttpServerHttpModeTest.java— HTTP integration test patterns


