--- name: crestapps-core-mcp-file-resources description: Skill for exposing FTP, FTPS and SFTP files as CrestApps.Core MCP resource types with the consolidated CrestApps.Core.AI.Ftp and CrestApps.Core.AI.Sftp packages. Covers AddFtpResources, AddSftpResources, AddCoreAIFtpMcpResources and AddCoreAISftpMcpResources, FtpResourceTypeHandler and SftpResourceTypeHandler, the path URI variable and its sanitization, FtpConnectionMetadata and SftpConnectionMetadata with Data Protection purposes, SFTP host key fingerprints with SftpHostKeys, SFTP proxies, the IRemoteFileEgressPolicy that pins each connection to checked addresses (PublicRemoteFileEgressPolicy), FtpOptions AllowAnyCertificate and AllowUnencrypted, MaxResourceBytes and RemoteFileResourceHandlerBase, the matching file source ingestion connectors in the same packages, and upgrading from CrestApps.Core.AI.Mcp.Ftp and CrestApps.Core.AI.Mcp.Sftp. license: Apache-2.0 metadata: author: CrestApps Team version: "1.0" --- # CrestApps.Core MCP File Resources Expose files on an FTP, FTPS or SFTP server to MCP clients as resources. In 2.0 each protocol lives in one package that also carries the file source ingestion connector for the same protocol (see `crestapps-core-file-sources`). Registering one side does not register the other. | Package | MCP resource registration | Ingestion connector registration | |---|---|---| | `CrestApps.Core.AI.Ftp` | `AddFtpResources()` / `AddCoreAIFtpMcpResources()` | `AddCoreFtpIngestionConnector()` | | `CrestApps.Core.AI.Sftp` | `AddSftpResources()` / `AddCoreAISftpMcpResources()` | `AddCoreSftpIngestionConnector()` | ## Register Both Resource Types ```csharp using CrestApps.Core.AI.Ftp; using CrestApps.Core.AI.Mcp; using CrestApps.Core.AI.Sftp; builder.Services.AddCrestAppsCore(crestApps => crestApps .AddAISuite(ai => ai .AddMcpServer(mcpServer => mcpServer .AddYesSqlStores() .AddFtpResources() .AddSftpResources()))); ``` Without the builder: ```csharp builder.Services .AddCoreAIMcpServer() .AddCoreAIFtpMcpResources() .AddCoreAISftpMcpResources(); ``` Both registration methods take an optional `Action` to change the display name, description or variables. The MCP server itself (SDK transport, `WithCrestAppsHandlers()`, `MapMcp`) is covered in `crestapps-core-mcp`. ## Handlers - `FtpResourceTypeHandler` serves type `ftp` (`FtpResourceConstants.Type`), using FluentFTP. - `SftpResourceTypeHandler` serves type `sftp` (`SftpResourceConstants.Type`), using SSH.NET. - Both expose one optional `path` variable, for example a resource URI template `ftp://reports/{path}`. An omitted value becomes `/`. - `SanitizePath` rejects null bytes and `.` or `..` segments, normalizes `\` to `/` and collapses empty segments, and the handler prefixes the result with `/`. - Content is returned as `TextResourceContents` with the resource's MIME type, or one detected from the file extension, falling back to `application/octet-stream`. - Both derive from `RemoteFileResourceHandlerBase`, which stops reading at the metadata's `MaxResourceBytes` (5 MB when not set) and answers with an error instead of buffering a larger file. Derive a custom remote-file handler from it to get the same limit; it also turns a `RemoteFileEgressDeniedException` into an error result with the overridable `EgressDeniedMessage` and logs the exception's own message. ## Attach FTP Metadata ```csharp using CrestApps.Core; using CrestApps.Core.AI.Ftp; using CrestApps.Core.AI.Ftp.Models; var protector = dataProtectionProvider.CreateProtector(FtpResourceConstants.DataProtectionPurpose); resource.Put(new FtpConnectionMetadata { Host = "files.example.com", Port = 21, Username = "reader", Password = protector.Protect(password), EncryptionMode = "Explicit", DataConnectionType = "AutoPassive", ConnectTimeout = 30, ReadTimeout = 30, RetryAttempts = 2, }); ``` A connection with a `Username` must use `EncryptionMode` `Explicit` or `Implicit`; unset, `None` and `Auto` can run in plain text and are refused unless the host sets `FtpOptions.AllowUnencrypted`. `ValidateAnyCertificate` is honored only when the host sets `FtpOptions.AllowAnyCertificate`; otherwise the connection is refused with `FtpOptions.AnyCertificateNotAllowedMessage`. Anonymous connections are not affected. Both checks are `FtpOptions.GetPolicyViolation(metadata)`, used by the resource handler and the ingestion connector: ```csharp using CrestApps.Core.AI.Ftp; // Development servers only. builder.Services.Configure(options => { options.AllowAnyCertificate = true; options.AllowUnencrypted = true; }); ``` ## Attach SFTP Metadata ```csharp using CrestApps.Core; using CrestApps.Core.AI.Sftp; using CrestApps.Core.AI.Sftp.Models; var protector = dataProtectionProvider.CreateProtector(SftpResourceConstants.DataProtectionPurpose); resource.Put(new SftpConnectionMetadata { Host = "sftp.example.com", Port = 22, Username = "reader", PrivateKey = protector.Protect(privateKeyPem), Passphrase = protector.Protect(passphrase), HostKeyFingerprint = "SHA256:47DEQpj8HBSa+/TImW+5JCeuQeRkm5NMpJWZG3hSuFU", // as ssh-keygen -lf prints it ConnectionTimeout = 30, KeepAliveInterval = 30, }); ``` The handler requires `Host`, at least one protected `Password` or `PrivateKey`, and `HostKeyFingerprint` (`SHA256:...` or the legacy MD5 hex form; separate several with commas to rotate a key). `SftpHostKeys` refuses the connection when no fingerprint is stored or the server presents a different key, so credentials never reach an unverified server (the factory throws `InvalidOperationException`, the handler returns an error result). It reads `Username` for the SSH authentication methods. `ProxyType`, `ProxyHost`, `ProxyPort`, `ProxyUsername` and `ProxyPassword` route the connection through a SOCKS4, SOCKS5 or HTTP proxy (see [Which Address a Connection Opens](#which-address-a-connection-opens)); leave `ProxyType` at `None` to connect directly. | Constant | Value | |---|---| | `FtpResourceConstants.DataProtectionPurpose` | `FtpResourcePassword` | | `SftpResourceConstants.DataProtectionPurpose` | `SftpResourceCredentials` | The ingestion connectors decrypt with the same purposes, so a file source and an MCP resource can share the same stored connection shape. A mismatched purpose reads back as no credential rather than an error. ## Which Address a Connection Opens FTP and SFTP connections (the MCP resource handlers and the ingestion connectors alike) ask `IRemoteFileEgressPolicy` (`CrestApps.Core.AI.FileSources.FileTransfer`) once for the addresses of the stored host and connect only to those, in order. Neither FluentFTP nor SSH.NET looks the name up again, so a name that changes its answer after the check cannot move the connection. - **FTP**: every passive data connection (`PASV`, `EPSV`, the automatic types) goes to the address the control connection opened, whatever the server's answer names. TLS still checks the certificate against the host name. An FTP server that hands out another address for data connections (some load-balanced clusters) no longer works. - **SFTP**: SSH.NET is given the address; the host key fingerprint still identifies the server. The proxy fields now take effect: `ProxyType` (`None`, `Socks4`, `Socks5` or `Http`), `ProxyHost`, `ProxyPort` (1 to 65535), `ProxyUsername` and `ProxyPassword`. The proxy's addresses go through the policy too, the proxy is asked for a checked address (never the name; an HTTP proxy gets IPv6 in brackets, a SOCKS4 proxy only IPv4), and an unknown type or a missing host or port is refused. `DefaultRemoteFileEgressPolicy` (registered with `TryAdd` by every FTP and SFTP `Add*` method) resolves the name and allows every address. When people who are not administrators can edit file sources or MCP resources, register `PublicRemoteFileEgressPolicy`, which allows only public internet addresses (it refuses loopback, private, `100.64.0.0/10`, link-local `169.254.0.0/16` and `fe80::/10`, `fc00::/7`, multicast and reserved addresses, and checks IPv4-mapped IPv6 as IPv4): ```csharp using CrestApps.Core.AI.FileSources.FileTransfer; builder.Services.AddSingleton(); ``` A custom policy implements `Task> ResolveAsync(string host, int port, CancellationToken cancellationToken)` and throws `RemoteFileEgressDeniedException` (`CrestApps.Core.AI.Exceptions`) when no address is allowed: ```csharp using System.Net; using CrestApps.Core.AI.Exceptions; using CrestApps.Core.AI.FileSources.FileTransfer; public sealed class KnownFileServersEgressPolicy : IRemoteFileEgressPolicy { private static readonly HashSet _allowed = [IPAddress.Parse("203.0.113.10"), IPAddress.Parse("203.0.113.11")]; private readonly DefaultRemoteFileEgressPolicy _resolver = new(); public async Task> ResolveAsync(string host, int port, CancellationToken cancellationToken = default) { var addresses = await _resolver.ResolveAsync(host, port, cancellationToken); var allowed = addresses .Select(address => address.IsIPv4MappedToIPv6 ? address.MapToIPv4() : address) .Where(_allowed.Contains) .ToArray(); if (allowed.Length == 0) { throw new RemoteFileEgressDeniedException($"Connections to '{host}' are not allowed."); } return allowed; } } ``` An MCP read that the policy refuses answers "This application does not allow FTP connections to this server." (or SFTP); the exception's own message only goes to the log. ## Resource Records Create an `McpResource` whose `Source` is `ftp` or `sftp` and whose `Resource.Uri` is a fixed URI or a template containing `{path}`, then `Put(...)` the connection metadata on it. The metadata is read at resource-read time and is never shown to the MCP client. Use an `IMcpResourceHandler.Exporting` handler to remove credentials when resources are exported. ## Upgrading Within 2.0 Previews - Store `HostKeyFingerprint` on every SFTP resource and file source, or those connections stop working. - Set `EncryptionMode` to `Explicit` or `Implicit` on FTP connections with credentials, or allow plain FTP with `FtpOptions.AllowUnencrypted`; `ValidateAnyCertificate` needs `FtpOptions.AllowAnyCertificate`. - `FtpRemoteFileClientFactory` and `FtpResourceTypeHandler` take an `IOptions`; only code that constructs them changes. - Reads above `MaxResourceBytes` (resources, 5 MB) or `MaxFileBytes` (ingestion, 100 MB) are refused instead of buffered. - FTP and SFTP connect only to the addresses `IRemoteFileEgressPolicy` returns, and FTP passive data connections stay on the control connection's address. The constructors of `FtpRemoteFileClientFactory`, `FtpResourceTypeHandler`, `SftpRemoteFileClientFactory` and `SftpResourceTypeHandler` take an `IRemoteFileEgressPolicy`. - A stored SFTP proxy type now takes effect; set `ProxyType` to `None` on resources that should connect directly. A proxy type other than `None`, `Socks4`, `Socks5` or `Http`, or one without a host or a port from 1 to 65535, is refused (an MCP read returns that as its result). - The MVC sample's MCP resource form saves SFTP resources again (its hidden FTP fields used to post empty `Host`, `Port`, `Username` and `Password` first). ## Upgrading from 1.x - Replace package references `CrestApps.Core.AI.Mcp.Ftp` and `CrestApps.Core.AI.Mcp.Sftp` with `CrestApps.Core.AI.Ftp` and `CrestApps.Core.AI.Sftp`. - Rename namespaces `CrestApps.Core.AI.Mcp.Ftp[.Models|.Handlers]` to `CrestApps.Core.AI.Ftp[.Models|.Handlers]`, and the same for SFTP. - Registration method names and behavior are unchanged. - Stored data needs no migration; metadata is stored under the short type names `FtpConnectionMetadata` and `SftpConnectionMetadata`.