0 people like it.

ModelContextProtocol with FSharp (MCP with F#)

ModelContextProtocol with FSharp (MCP with F#) https://modelcontextprotocol.io/docs/getting-started/intro Client and Server examples using https://github.com/modelcontextprotocol/csharp-sdk

  1: 
  2: 
  3: 
  4: 
  5: 
  6: 
  7: 
  8: 
  9: 
 10: 
 11: 
 12: 
 13: 
 14: 
 15: 
 16: 
 17: 
 18: 
 19: 
 20: 
 21: 
 22: 
 23: 
 24: 
 25: 
 26: 
 27: 
 28: 
 29: 
 30: 
 31: 
 32: 
 33: 
 34: 
 35: 
 36: 
 37: 
 38: 
 39: 
 40: 
 41: 
 42: 
 43: 
 44: 
 45: 
 46: 
 47: 
 48: 
 49: 
 50: 
 51: 
 52: 
 53: 
 54: 
 55: 
 56: 
 57: 
 58: 
 59: 
 60: 
 61: 
 62: 
 63: 
 64: 
 65: 
 66: 
 67: 
 68: 
 69: 
 70: 
 71: 
 72: 
 73: 
 74: 
 75: 
 76: 
 77: 
 78: 
 79: 
 80: 
 81: 
 82: 
 83: 
 84: 
 85: 
 86: 
 87: 
 88: 
 89: 
 90: 
 91: 
 92: 
 93: 
 94: 
 95: 
 96: 
 97: 
 98: 
 99: 
100: 
101: 
102: 
103: 
104: 
105: 
106: 
107: 
108: 
109: 
110: 
111: 
112: 
113: 
module McpServer = 

    // #r "nuget: ModelContextProtocol,0.4.0-preview.1"
    // #r "nuget: Microsoft.Extensions.Hosting"

    open Microsoft.Extensions.DependencyInjection
    open Microsoft.Extensions.Hosting
    open Microsoft.Extensions.Logging
    open ModelContextProtocol.Server
    open System
    open System.ComponentModel


    [<McpServerToolType>]
    type EchoTool =
        [<McpServerTool; 
        Description "Echoes the message back to the client.">]
        static member Echo (message:string) = $"Hello from F#: {message}"

    // Define your urls whatever .NET Core style you like....
    let args = [|"--urls \"http://localhost:5000\""|]

    let builder = Host.CreateApplicationBuilder args

    builder.Logging.AddConsole(fun consoleLogOptions ->
        // Configure all logs to go to stderr
        consoleLogOptions.LogToStandardErrorThreshold <- LogLevel.Trace
        )

    builder.Services
        .AddMcpServer(fun opts -> 
            opts.ServerInfo <- ModelContextProtocol.Protocol.Implementation(
                Name = "MyServer", Version = "1.0", 
                Title = "My echo server"
            )
        )
        .WithStdioServerTransport()
        .WithToolsFromAssembly()


    builder.Build().RunAsync()
    |> System.Threading.Tasks.Task.WaitAny

    // Test in VSCode CoPilot:
    // Create .vscode\mcp.json file, you can make it like this:
    // VSCode: Press Ctrl + Shift + P -> Command (stdio), Command to run:
    // dotnet fsi c:\mypath\Server.fsx
    // Then Ctrl + Alt + I -> Agents mode, should be available now.

module Server = 

    // #r "nuget: ModelContextProtocol,0.4.0-preview.1"
    // #r "nuget: Microsoft.Extensions.Hosting"

    open Microsoft.Extensions.DependencyInjection
    open Microsoft.Extensions.Hosting
    open Microsoft.Extensions.Logging
    open ModelContextProtocol.Server
    open System
    open System.ComponentModel


    [<McpServerToolType>]
    type EchoTool =
        [<McpServerTool; 
        Description "Echoes the message back to the client.">]
        static member Echo (message:string) = $"Hello from F#: {message}"

    // Define your urls whatever .NET Core style you like....
    let args = [|"--urls \"http://localhost:5000\""|]

    let builder = Host.CreateApplicationBuilder args

    builder.Logging.AddConsole(fun consoleLogOptions ->
        // Configure all logs to go to stderr
        consoleLogOptions.LogToStandardErrorThreshold <- LogLevel.Trace
        )

    builder.Services
        .AddMcpServer(fun opts -> 
            opts.ServerInfo <- ModelContextProtocol.Protocol.Implementation(
                Name = "MyServer", Version = "1.0", 
                Title = "My echo server"
            )
        )
        .WithStdioServerTransport()
        .WithToolsFromAssembly()


    builder.Build().RunAsync()
    |> System.Threading.Tasks.Task.WaitAny

    // Test in VSCode CoPilot:
    // Create .vscode\mcp.json file, you can make it like this:
    // VSCode: Press Ctrl + Shift + P -> Command (stdio), Command to run:
    // dotnet fsi c:\mypath\Server.fsx
    // Then Ctrl + Alt + I -> Agents mode, should be available now.

(* Content of generated mcp.json example:
{
	"servers": {
		"MyServer": {
			"type": "stdio",
			"command": "dotnet",
			"args": [
				"fsi",
				"C:\\git\\mcp\\Server.fsx"
			]
		}
	},
	"inputs": []
}
*)
namespace Microsoft
namespace System
namespace System.ComponentModel
Multiple items
type DescriptionAttribute =
  inherit Attribute
  new : unit -> unit + 1 overload
  member Equals : obj: obj -> bool
  member GetHashCode : unit -> int
  member IsDefaultAttribute : unit -> bool
  val ``<DescriptionValue>k__BackingField`` : string
  static val Default : DescriptionAttribute
  member Description : string
  member DescriptionValue : string

--------------------
DescriptionAttribute() : DescriptionAttribute
DescriptionAttribute(description: string) : DescriptionAttribute
val message : string
Multiple items
val string : value:'T -> string

--------------------
type string = String
val args : string []
val builder : obj
Multiple items
type Version =
  interface ICloneable
  interface IComparable
  interface IComparable<Version>
  interface IEquatable<Version>
  interface ISpanFormattable
  new : major: int * minor: int * build: int * revision: int -> unit + 5 overloads
  member Clone : unit -> obj
  member CompareTo : version: obj -> int + 1 overload
  member Equals : obj: obj -> bool + 1 overload
  member GetHashCode : unit -> int
  ...

--------------------
Version() : Version
Version(version: string) : Version
Version(major: int, minor: int) : Version
Version(major: int, minor: int, build: int) : Version
Version(major: int, minor: int, build: int, revision: int) : Version
namespace System.Threading
namespace System.Threading.Tasks
Multiple items
type Task =
  interface IAsyncResult
  interface IDisposable
  new : canceled: bool * creationOptions: TaskCreationOptions * ct: CancellationToken -> unit + 11 overloads
  member AddCompletionAction : action: ITaskCompletionAction -> unit + 1 overload
  member AddException : exceptionObject: obj -> unit + 1 overload
  member AddExceptionsFromChildren : props: ContingentProperties -> unit
  member AddNewChild : unit -> unit
  member AddTaskContinuation : tc: obj * addBeforeOthers: bool -> bool
  member AddTaskContinuationComplex : tc: obj * addBeforeOthers: bool -> bool
  member AssignCancellationToken : cancellationToken: CancellationToken * antecedent: Task * continuation: TaskContinuation -> unit
  ...

--------------------
type Task<'TResult> =
  inherit Task
  new : unit -> unit + 13 overloads
  member ConfigureAwait : continueOnCapturedContext: bool -> ConfiguredTaskAwaitable<'TResult>
  member ContinueWith : continuationAction: Action<Task<'TResult>> -> Task + 23 overloads
  member DangerousSetResult : result: 'TResult -> unit
  member GetAwaiter : unit -> TaskAwaiter<'TResult>
  member GetResultCore : waitCompletionNotification: bool -> 'TResult
  member InnerInvoke : unit -> unit
  member TrySetResult : result: 'TResult -> bool
  static member StartNew : parent: Task * function: Func<'TResult> * cancellationToken: CancellationToken * creationOptions: TaskCreationOptions * internalOptions: InternalTaskOptions * scheduler: TaskScheduler -> Task<'TResult> + 1 overload
  ...

--------------------
Threading.Tasks.Task(action: Action) : Threading.Tasks.Task
Threading.Tasks.Task(action: Action, cancellationToken: Threading.CancellationToken) : Threading.Tasks.Task
Threading.Tasks.Task(action: Action, creationOptions: Threading.Tasks.TaskCreationOptions) : Threading.Tasks.Task
Threading.Tasks.Task(action: Action<obj>, state: obj) : Threading.Tasks.Task
Threading.Tasks.Task(action: Action, cancellationToken: Threading.CancellationToken, creationOptions: Threading.Tasks.TaskCreationOptions) : Threading.Tasks.Task
Threading.Tasks.Task(action: Action<obj>, state: obj, cancellationToken: Threading.CancellationToken) : Threading.Tasks.Task
Threading.Tasks.Task(action: Action<obj>, state: obj, creationOptions: Threading.Tasks.TaskCreationOptions) : Threading.Tasks.Task
Threading.Tasks.Task(action: Action<obj>, state: obj, cancellationToken: Threading.CancellationToken, creationOptions: Threading.Tasks.TaskCreationOptions) : Threading.Tasks.Task

--------------------
Threading.Tasks.Task(function: Func<'TResult>) : Threading.Tasks.Task<'TResult>
Threading.Tasks.Task(function: Func<'TResult>, cancellationToken: Threading.CancellationToken) : Threading.Tasks.Task<'TResult>
Threading.Tasks.Task(function: Func<'TResult>, creationOptions: Threading.Tasks.TaskCreationOptions) : Threading.Tasks.Task<'TResult>
Threading.Tasks.Task(function: Func<obj,'TResult>, state: obj) : Threading.Tasks.Task<'TResult>
Threading.Tasks.Task(function: Func<'TResult>, cancellationToken: Threading.CancellationToken, creationOptions: Threading.Tasks.TaskCreationOptions) : Threading.Tasks.Task<'TResult>
Threading.Tasks.Task(function: Func<obj,'TResult>, state: obj, cancellationToken: Threading.CancellationToken) : Threading.Tasks.Task<'TResult>
Threading.Tasks.Task(function: Func<obj,'TResult>, state: obj, creationOptions: Threading.Tasks.TaskCreationOptions) : Threading.Tasks.Task<'TResult>
Threading.Tasks.Task(function: Func<obj,'TResult>, state: obj, cancellationToken: Threading.CancellationToken, creationOptions: Threading.Tasks.TaskCreationOptions) : Threading.Tasks.Task<'TResult>
Threading.Tasks.Task.WaitAny([<ParamArray>] tasks: Threading.Tasks.Task []) : int
Threading.Tasks.Task.WaitAny(tasks: Threading.Tasks.Task [], millisecondsTimeout: int) : int
Threading.Tasks.Task.WaitAny(tasks: Threading.Tasks.Task [], cancellationToken: Threading.CancellationToken) : int
Threading.Tasks.Task.WaitAny(tasks: Threading.Tasks.Task [], timeout: TimeSpan) : int
Threading.Tasks.Task.WaitAny(tasks: Threading.Tasks.Task [], millisecondsTimeout: int, cancellationToken: Threading.CancellationToken) : int
module Server

from Script

More information

Link:http://fssnip.net/8aN
Posted:9 hours ago
Author:Tuomas Hietanen
Tags: modelcontextprotocol , llm