A RetroSearch Logo

Home - News ( United States | United Kingdom | Italy | Germany ) - Football scores

Search Query:

Showing content from https://github.com/nanoframework/nanoFramework.WebServer below:

nanoframework/nanoFramework.WebServer: :package: Web server for .NET nanoFramework packed with features: REST api using attributes, multithread requests, parameters in query URL, static files serving.

.NET nanoFramework WebServer with Model Context Protocol (MCP)

This library provides a lightweight, multi-threaded HTTP/HTTPS WebServer for .NET nanoFramework with comprehensive Model Context Protocol (MCP) support for AI agent integration.

Basic Event Based WebServer

Using the Web Server is very straight forward and supports event based calls.

// You need to be connected to a wifi or ethernet connection with a proper IP Address

using (WebServer server = new WebServer(80, HttpProtocol.Http))
{
    server.CommandReceived += ServerCommandReceived;
    server.Start();
    Thread.Sleep(Timeout.Infinite);
}

private static void ServerCommandReceived(object source, WebServerEventArgs e)
{
    if (e.Context.Request.RawUrl.ToLower() == "/hello")
    {
        WebServer.OutPutStream(e.Context.Response, "Hello from nanoFramework!");
    }
    else
    {
        WebServer.OutputHttpCode(e.Context.Response, HttpStatusCode.NotFound);
    }
}
Controller-Based WebServer

Controllers are supported including with parametarized routes like 'api/led/{id}/dosomething/{order}'.

using (WebServer server = new WebServer(80, HttpProtocol.Http, new Type[] { typeof(MyController) }))
{
    server.Start();
    Thread.Sleep(Timeout.Infinite);
}

public class MyController
{
    [Route("api/hello")]
    [Method("GET")]
    public void Hello(WebServerEventArgs e)
    {
        WebServer.OutPutStream(e.Context.Response, "Hello from Controller!");
    }

    [Route("api/led/{id}")]
    [Method("GET")]
    public void LedState(WebServerEventArgs e)
    {
        string ledId = e.GetRouteParameter("id");
        WebServer.OutPutStream(e.Context.Response, $"You selected Led {ledId}!");
    }
}
Model Context Protocol (MCP) Support

Enable AI agents to interact with your embedded devices through standardized tools and JSON-RPC 2.0 protocol.

public class IoTTools
{
    [McpServerTool("read_sensor", "Reads temperature from sensor")]
    public static string ReadTemperature()
    {
        // Your sensor reading code
        return "23.5°C";
    }

    [McpServerTool("control_led", "Controls device LED", "Uutput the statusof the LED")]
    public static string ControlLed(LedCommand command)
    {
        // Your LED control code
        return $"LED set to {command.State}";
    }
}

public class LedCommand
{
    [Description("LED state: on, off, or blink")]
    public string State { get; set; }
}

You can define reusable, high-level prompts for AI agents using the McpServerPrompt attribute. Prompts encapsulate multi-step instructions or workflows that can be invoked by agents.

Here's a simple example:

using nanoFramework.WebServer.Mcp;

public class McpPrompts
{
    [McpServerPrompt("echo_sanity_check", "Echo test prompt")]
    public static PromptMessage[] EchoSanityCheck()
    {
        return new PromptMessage[]
        {
            new PromptMessage("Call Echo with the string 'Hello MCP world!' and return the response.")
        };
    }
}

Prompts can be discovered and invoked by AI agents in the same way as tools. You can also define prompts with parameters using the McpPromptParameter attribute.

public static void Main()
{
    // Connect to WiFi first
    var connected = WifiNetworkHelper.ConnectDhcp(Ssid, Password, requiresDateTime: true);
    
    // Discover and register MCP tools
    McpToolRegistry.DiscoverTools(new Type[] { typeof(IoTTools) });

    // Discover and register MCP prompts
    McpPromptRegistry.DiscoverPrompts(new Type[] { typeof(McpPrompts) });
    
    // Start WebServer with MCP support
    using (var server = new WebServer(80, HttpProtocol.Http, new Type[] { typeof(McpServerController) }))
    {
        // Optional customization
        McpServerController.ServerName = "MyIoTDevice";
        McpServerController.Instructions = "IoT device with sensor and LED control capabilities.";
        
        server.Start();
        Thread.Sleep(Timeout.Infinite);
    }
}

Once running, AI agents can discover and invoke your tools:

// Tool discovery
POST /mcp
{
    "jsonrpc": "2.0",
    "method": "tools/list",
    "id": 1
}

// Tool invocation
POST /mcp
{
    "jsonrpc": "2.0",
    "method": "tools/call",
    "params": {
        "name": "control_led",
        "arguments": {"State": "on"}
    },
    "id": 2
}

Install 'nanoFramework.WebServer' for the Web Server without File System support. Install 'nanoFramework.WebServer.FileSystem' for file serving, so with devices supporting File System. Install 'nanoFramework.WebServer.Mcp' for MCP support. It does contains the full 'nanoFramework.WebServer' but does not include native file serving. You can add this feature fairly easilly by reusing the code function serving it.

For documentation, feedback, issues and contributions, please refer to the Home repo.

Join our Discord community here.

The list of contributors to this project can be found at CONTRIBUTORS.

Licensed under the MIT license.

This project has adopted the code of conduct defined by the Contributor Covenant to clarify expected behaviour in our community. For more information see the .NET Foundation Code of Conduct.

This project is supported by the .NET Foundation.


RetroSearch is an open source project built by @garambo | Open a GitHub Issue

Search and Browse the WWW like it's 1997 | Search results from DuckDuckGo

HTML: 3.2 | Encoding: UTF-8 | Version: 0.7.4