Skip to content
Merged
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
54 changes: 41 additions & 13 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
[![Quality Gate Status](https://sonarcloud.io/api/project_badges/measure?project=nanoframework_lib-nanoframework.WebServer&metric=alert_status)](https://sonarcloud.io/dashboard?id=nanoframework_lib-nanoframework.WebServer) [![Reliability Rating](https://sonarcloud.io/api/project_badges/measure?project=nanoframework_lib-nanoframework.WebServer&metric=reliability_rating)](https://sonarcloud.io/dashboard?id=nanoframework_lib-nanoframework.WebServer) [![NuGet](https://img.shields.io/nuget/dt/nanoFramework.WebServer.svg?label=NuGet&style=flat&logo=nuget)](https://www.nuget.org/packages/nanoFramework.WebServer/) [![#yourfirstpr](https://img.shields.io/badge/first--timers--only-friendly-blue.svg)](https://github.com/nanoframework/Home/blob/main/CONTRIBUTING.md) [![Discord](https://img.shields.io/discord/478725473862549535.svg?logo=discord&logoColor=white&label=Discord&color=7289DA)](https://discord.gg/gCyBu8T)
[![Quality Gate Status](https://sonarcloud.io/api/project_badges/measure?project=nanoframework_lib-nanoframework.WebServer&metric=alert_status)](https://sonarcloud.io/dashboard?id=nanoframework_lib-nanoframework.WebServer) [![Reliability Rating](https://sonarcloud.io/api/project_badges/measure?project=nanoframework_lib-nanoframework.WebServer&metric=reliability_rating)](https://sonarcloud.io/dashboard?id=nanoframework_lib-nanoframework.WebServer) [![NuGet](https://img.shields.io/nuget/dt/nanoFramework.WebServer.svg?label=NuGet&style=flat&logo=nuget)](https://www.nuget.org/packages/nanoFramework.WebServer/) [![#yourfirstpr](https://img.shields.io/badge/first--timers--only-friendly-blue.svg)](https://github.com/nanoframework/Home/blob/main/CONTRIBUTING.md) [![Discord](https://img.shields.io/discord/478725473862549535.svg?logo=discord&logoColor=white&label=Discord&color=7289DA)](https://discord.gg/gCyBu8T)

![nanoFramework logo](https://raw.githubusercontent.com/nanoframework/Home/main/resources/logo/nanoFramework-repo-logo.png)

Expand Down Expand Up @@ -35,7 +35,7 @@ This library provides a lightweight, multi-threaded HTTP/HTTPS WebServer for .NE

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

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

using (WebServer server = new WebServer(80, HttpProtocol.Http))
Expand All @@ -56,13 +56,13 @@ private static void ServerCommandReceived(object source, WebServerEventArgs e)
WebServer.OutputHttpCode(e.Context.Response, HttpStatusCode.NotFound);
}
}
```
'''

### Controller-Based WebServer

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

```csharp
'''csharp
using (WebServer server = new WebServer(80, HttpProtocol.Http, new Type[] { typeof(MyController) }))
{
server.Start();
Expand All @@ -86,15 +86,15 @@ public class MyController
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.

### Defining MCP Tools

```csharp
'''csharp
public class IoTTools
{
[McpServerTool("read_sensor", "Reads temperature from sensor")]
Expand All @@ -117,18 +117,45 @@ public class LedCommand
[Description("LED state: on, off, or blink")]
public string State { get; set; }
}
'''

### Defining MCP Prompts

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:

```csharp
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.

### Setting Up MCP Server

```csharp
'''csharp
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) }))
Expand All @@ -141,13 +168,13 @@ public static void Main()
Thread.Sleep(Timeout.Infinite);
}
}
```
'''

### AI Agent Integration

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

```json
'''json
// Tool discovery
POST /mcp
{
Expand All @@ -167,7 +194,7 @@ POST /mcp
},
"id": 2
}
```
'''

## Documentation

Expand All @@ -187,11 +214,12 @@ POST /mcp
- No compression support in request/response streams
- MCP implementation supports server features only (no notifications or SSE)
- No or single parameter limitation for MCP tools (use complex objects for multiple parameters)
- Prompt parameters, when declared, are always mandatory.

## Installation

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.
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.

## Contributing

Expand Down
190 changes: 185 additions & 5 deletions doc/model-context-protocol.md
Original file line number Diff line number Diff line change
Expand Up @@ -27,14 +27,50 @@ The Model Context Protocol (MCP) is an open standard that enables seamless integ

### Key Features

- **Automatic tool discovery** through reflection and attributes
- **Automatic tool and prompt discovery** through reflection and attributes
- **MCP Prompts**: Define reusable, high-level prompt workflows for AI agents, with support for parameters
- **JSON-RPC 2.0 compliant** request/response handling
- **Type-safe parameter handling** with automatic deserialization from JSON to .NET objects
- **Flexible authentication** options (none, basic auth, API key)
- **Complex object support** for both input parameters and return values
- **Robust error handling** and validation
- **Memory efficient** implementation optimized for embedded devices
- **HTTPS support** with SSL/TLS encryption
## Defining MCP Prompts

MCP Prompts allow you to define reusable, multi-step instructions or workflows that can be invoked by AI agents. Prompts are discovered and registered similarly to tools, using the `[McpServerPrompt]` attribute on static methods that return an array of `PromptMessage`.

Prompts can encapsulate complex logic, multi-step flows, or provide high-level instructions for agents. You can also define parameters for prompts using the `[McpPromptParameter]` attribute. **All parameters defined for a prompt are mandatory.**

### Example: Defining a Prompt

```csharp
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.")
};
}

[McpServerPrompt("summarize_person", "Summarize a person with age threshold")]
[McpPromptParameter("ageThreshold", "The age threshold to determine if the person is a senior or junior.")]
public static PromptMessage[] SummarizePerson(string ageThreshold)
{
return new PromptMessage[]
{
new PromptMessage($"Call GetDefaultPerson, then if person.Age > {ageThreshold} label as senior, else junior.")
};
}
}
```

Prompts are listed and invoked via the MCP protocol, just like tools.

### Supported Version

Expand Down Expand Up @@ -309,6 +345,11 @@ public static void Main()
typeof(SensorTools)
});

// Discover and register prompts
McpPromptRegistry.DiscoverPrompts(new Type[] {
typeof(McpPrompts)
});

// Step 3: Start WebServer with MCP support
using (var server = new WebServer(80, HttpProtocol.Http, new Type[] { typeof(McpServerController) }))
{
Expand Down Expand Up @@ -482,7 +523,8 @@ POST /mcp
}
```

### 2. Tool Discovery

### 2. Tool and Prompt Discovery

Agent discovers available tools:

Expand All @@ -495,7 +537,18 @@ POST /mcp
}
```

The response will be the list of the tools. See next section for detailed examples.
And prompts:

```json
POST /mcp
{
"jsonrpc": "2.0",
"method": "prompts/list",
"id": 2
}
```

There will be responses for tools and prompts. See next section for detailed examples.

### 3. Tool Invocation

Expand All @@ -519,15 +572,15 @@ POST /mcp

## Request/Response Examples

This section shows real exampled of requests and responses.
This section shows real examples of requests and responses.


### Tool Discovery

**Request:**

```json
POST /mcp

{
"jsonrpc": "2.0",
"method": "tools/list",
Expand Down Expand Up @@ -611,6 +664,61 @@ POST /mcp
}
```

### Prompt Discovery

**Request:**

```json
POST /mcp
{
"jsonrpc": "2.0",
"method": "prompts/list",
"id": 1
}
```

**Response:**

```json
{
"jsonrpc": "2.0",
"id": 1,
"result": {
"prompts": [
{
"name": "echo_sanity_check",
"description": "Echo test prompt",
"parameters": [],
"messages": [
{
"role": "system",
"content": "Call Echo with the string 'Hello MCP world!' and return the response."
}
]
},
{
"name": "summarize_person",
"description": "Summarize a person with age threshold",
"parameters": [
{
"name": "ageThreshold",
"description": "The age threshold to determine if the person is a senior or junior.",
"type": "string"
}
],
"messages": [
{
"role": "system",
"content": "Call GetDefaultPerson, then if person.Age > {ageThreshold} label as senior, else junior."
}
]
}
],
"nextCursor": null
}
}
```

### Simple Tool Invocation

**Request:**
Expand Down Expand Up @@ -714,6 +822,78 @@ POST /mcp
}
```

### Prompt usage

#### Prompt Retrieval Example

To retrieve a prompt, use the `prompts/get` method. Provide the prompt name and any required parameters.

**Request:**

```json
POST /mcp
{
"jsonrpc": "2.0",
"method": "prompts/get",
"params": {
"name": "echo_sanity_check",
"arguments": {}
},
"id": 5
}
```

**Response:**

```json
{
"jsonrpc": "2.0",
"id": 5,
"result": {
"messages": [
{
"role": "system",
"content": "Call Echo with the string 'Hello MCP world!' and return the response."
}
]
}
}
```

If the prompt requires parameters, include them in the `arguments` object:

```json
POST /mcp
{
"jsonrpc": "2.0",
"method": "prompts/get",
"params": {
"name": "summarize_person",
"arguments": {
"ageThreshold": "65"
}
},
"id": 6
}
```

**Response:**

```json
{
"jsonrpc": "2.0",
"id": 6,
"result": {
"messages": [
{
"role": "system",
"content": "Call GetDefaultPerson, then if person.Age > 65 label as senior, else junior."
}
]
}
}
```

## Error Handling

The .NET nanoFramework MCP Server knows how to handle properly errors. The following will show examples of request and error responses.
Expand Down
Loading